TimerTask inside Runnable?

I

iksrazal

I want to do something like the following:

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.Executor;
import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;

public class PoolTest
{
class MyRunnable implements Runnable
{
public void run()
{
java.util.TimerTask task = new TimerTask()
{
Thread thread = Thread.currentThread();
public void run()
{
thread.interrupt(); // interrupt work
}
};

Timer timer = new Timer();
timer.schedule(task, 3000);
try
{
// do interruptible work ...
System.out.println("Inside MyRunnable...");
}
finally
{
task.cancel();
Thread.interrupted(); // clear interrupt flag
}
}
}

public static void main(String args[])
{
new PoolTest();
}

public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

The thread pool calls start() itself, so I can't call
TimerTask.start() . This code never exits.

Any ideas?
iksrazal
 
L

Liz

iksrazal said:
I want to do something like the following:

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.Executor;
import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;

public class PoolTest
{
class MyRunnable implements Runnable
{
public void run()
{
java.util.TimerTask task = new TimerTask()
{
Thread thread = Thread.currentThread();
public void run()
{
thread.interrupt(); // interrupt work
}
};

Timer timer = new Timer();
timer.schedule(task, 3000);
try
{
// do interruptible work ...
System.out.println("Inside MyRunnable...");
}
finally
{
task.cancel();
Thread.interrupted(); // clear interrupt flag
}
}
}

public static void main(String args[])
{
new PoolTest();
}

public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

The thread pool calls start() itself, so I can't call
TimerTask.start() . This code never exits.

Any ideas?
iksrazal

kludge this idea
---
// class to create a JLable and put the time of day into it (refresh every
1 second)
static class ClockLabel extends JLabel implements ActionListener
{
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
ClockLabel(String text, int horizontalAlignment)
{
super(text, horizontalAlignment);
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent e)
{
setText((formatter.format(new Date())).toString());
}
}
 
I

iksrazal

Liz said:
kludge this idea
---
// class to create a JLable and put the time of day into it (refresh every
1 second)
static class ClockLabel extends JLabel implements ActionListener
{
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
ClockLabel(String text, int horizontalAlignment)
{
super(text, horizontalAlignment);
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent e)
{
setText((formatter.format(new Date())).toString());
}
}

That's javax.swing.timer . I was refering to java.util.timer , but
thanks for trying.

Still need help,
iksrazal
 
I

iksrazal

(e-mail address removed) (iksrazal) wrote in message
This damn near killed me, but as usual the solution was simple. Thanks
to a nice fellow over at java ranch, this is how it turned out (just
in case someone else has the same problem someday):

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.*;

public class PoolTest
{
class TimeOutTask extends TimerTask
{
Thread t;

TimeOutTask(Thread t)
{
this.t = t;
}

public void run()
{
if(t!= null && t.isAlive())
{
t.interrupt();
}
}
}

class MyRunnable implements Runnable
{
//set as true to be a daemon thread and therefore exit on
interrupt
Timer timer = new Timer(true);

public void run()
{
timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
try
{
System.out.println("MyRunnable...");
Thread.sleep(10000);
}
catch (InterruptedException ie)
{
System.out.println("MyRunnable error...");
ie.printStackTrace();
}
}
}

public static void main(String args[])
{
new PoolTest();
}

public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

iksrazal
 
I

iksrazal

(e-mail address removed) (iksrazal) wrote in message
This damn near killed me, but as usual the solution was simple. Thanks
to a nice fellow over at java ranch, this is how it turned out (just
in case someone else has the same problem someday):

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.*;

public class PoolTest
{
class TimeOutTask extends TimerTask
{
Thread t;

TimeOutTask(Thread t)
{
this.t = t;
}

public void run()
{
if(t!= null && t.isAlive())
{
t.interrupt();
}
}
}

class MyRunnable implements Runnable
{
//set as true to be a daemon thread and therefore exit on
interrupt
Timer timer = new Timer(true);

public void run()
{
timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
try
{
System.out.println("MyRunnable...");
Thread.sleep(10000);
}
catch (InterruptedException ie)
{
System.out.println("MyRunnable error...");
ie.printStackTrace();
}
}
}

public static void main(String args[])
{
new PoolTest();
}

public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

iksrazal
 
I

iksrazal

(e-mail address removed) (iksrazal) wrote in message
This damn near killed me, but as usual the solution was simple. Thanks
to a nice fellow over at java ranch, this is how it turned out (just
in case someone else has the same problem someday):

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.*;

public class PoolTest
{
class TimeOutTask extends TimerTask
{
Thread t;

TimeOutTask(Thread t)
{
this.t = t;
}

public void run()
{
if(t!= null && t.isAlive())
{
t.interrupt();
}
}
}

class MyRunnable implements Runnable
{
//set as true to be a daemon thread and therefore exit on
interrupt
Timer timer = new Timer(true);

public void run()
{
timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
try
{
System.out.println("MyRunnable...");
Thread.sleep(10000);
}
catch (InterruptedException ie)
{
System.out.println("MyRunnable error...");
ie.printStackTrace();
}
}
}

public static void main(String args[])
{
new PoolTest();
}

public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

iksrazal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top