TimerTask inside Runnable?

5370 views
Skip to first unread message

iksrazal

unread,
Jun 29, 2004, 2:30:11 PM6/29/04
to
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

Liz

unread,
Jun 29, 2004, 3:57:48 PM6/29/04
to

"iksrazal" <iksr...@terra.com.br> wrote in message
news:850ed936.04062...@posting.google.com...

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());
}
}


iksrazal

unread,
Jul 1, 2004, 11:24:02 AM7/1/04
to
"Liz" <L...@nospam.com> wrote in message news:<0RjEc.3483$AI.2726@attbi_s04>...

> 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

iksrazal

unread,
Jul 1, 2004, 6:02:21 PM7/1/04
to
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

iksrazal

unread,
Jul 1, 2004, 6:02:48 PM7/1/04
to
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

iksrazal

unread,
Jul 1, 2004, 6:03:10 PM7/1/04
to
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

contac...@gmail.com

unread,
Feb 15, 2016, 8:42:17 PM2/15/16
to
Hey iksrazal this is what im looking for Excellent for posting this . You really made my day.Thanks a Tonnnnnnn. Keep it up!

Thanks,
Kiran
Reply all
Reply to author
Forward
0 new messages