This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: java.util.TimerTask


Hi,

On Thu, 2003-06-19 at 17:58, Erik Poupaert wrote:
> 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);
>
> Is my interpretation correct that the code above should keep on running?

Yes, and does seem to work for me. Maybe you are initializing the Timer
as daemon? In that case the program ends if the Timer is the only
running Thread. Or could you send a complete (non-)working example
program?

> 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.

Do you have a synchronized(this) around this loop?
Otherwise it might just be throwing MonitorStateExceptions inside the
loop. Try using Thread.sleep() otherwise.

Cheers,

Mark


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]