java.util.TimerTask
Andrew Haley
aph@redhat.com
Thu Jun 19 14:18:00 GMT 2003
Erik Poupaert writes:
>
> Hi
>
> Does anybody know how java.util.Timer is supposed to work? I think I'm
> doing something wrong, but I'm not really sure as to exactly what:
>
> mTimer=new Timer();
> mQueueTask=new QueueTask(); //inherits from TimerTask
> //and does something
> mTimer.schedule(mQueueTask,0,pIntervalMillis);
>
> The fact that a task has been scheduled to run, doesn't keep the
> executable running. It exits anyway.
>
> The JDK documentation, however, suggests that the executable should stay
> alive.
>
> http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
>
> "By default, a program keeps running as long as its timer threads are
> running."
>
> Is my interpretation correct that the code above should keep on running?
This:
import java.util.*;
class QueueTask extends TimerTask
{
public void run() { }
}
public class x
{
public static void main (String s[])
{
Timer mTimer=new Timer();
QueueTask mQueueTask=new QueueTask();
//and does something
mTimer.schedule(mQueueTask,0,100);
}
}
works correctly AFAICS.
> When I add the following workaround, the executable doesn't exit any
> longer, but indeed stays alive:
>
> // while(true)
> // {
> // try
> // {
> // wait(10000);
> // }
> // catch(Exception e)
> // {
> // }
> // }
>
> The only issue with this way of keeping the executable alive, is the
> fact that it consumes exorbitant amounts of CPU. I don't know why but,
> "wait(10000)" seems to be very expensive in terms of processor capacity.
What platform is this? Windows uses WaitForMultipleObjects(), posix
uses pthread_cond_timedwait(). Neither should eat CPU.
Andrew.
More information about the Java
mailing list