Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TimerTask inside Runnable?

Reply
Thread Tools

TimerTask inside Runnable?

 
 
iksrazal
Guest
Posts: n/a
 
      06-29-2004
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
 
Reply With Quote
 
 
 
 
Liz
Guest
Posts: n/a
 
      06-29-2004

"iksrazal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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());
}
}


 
Reply With Quote
 
 
 
 
iksrazal
Guest
Posts: n/a
 
      07-01-2004
"Liz" <(E-Mail Removed)> 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
 
Reply With Quote
 
iksrazal
Guest
Posts: n/a
 
      07-01-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (iksrazal) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
iksrazal
Guest
Posts: n/a
 
      07-01-2004
(E-Mail Removed) (iksrazal) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
iksrazal
Guest
Posts: n/a
 
      07-01-2004
(E-Mail Removed) (iksrazal) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bounded-Buffer with a TimerTask antonioatt@yahoo.it Java 0 06-21-2007 08:12 AM
TimerTask, scheduleAtFixedRate doesn't work parallel tkonrath Java 4 10-17-2005 10:45 AM
TimerTask destruction Jeff Java 4 08-20-2004 01:26 AM
TimerTask priority Liz Java 3 04-17-2004 11:55 PM
Re: ClassNotFoundException: java.util.TimerTask John C. Bollinger Java 0 08-18-2003 03:57 PM



Advertisments