How to terminate TimerThread in Timer

2019-08-16 16:44发布

问题:

When I read the source code of java.util.Timer , I found that java.util.Timer is implemented by a TimerThread and a TaskQueue

It is said that java.util.Timer can be cancelled by java.util.Timer.cancel(),

But when I call java.util.Timer.cancel() I found TimerThread in Timer still running ,

Does this a problem for online system ?

And if possible , how can I stop TimerThread inside java.util.Timer?


From source of TimerThread (jdk 7) I finally found that Timer.cancel() do works.

TimerThread wait TaskQueue , when Timer.cancel() called , TaskQueue notify TimerThread , And TimerThead should finally exit if `TaskQueue is EMPTY...

Thank you for your comment , sorry to wasting your time.

回答1:

Timer thread will still be running. But the timer thread will stop accepting anything into the queue as per the implementation.

The timer(TimerThread) thread is accessible to you only via Timer class. TimerThread is encapsultaed inside Timer class. Implementation wise, they have made sure the Timer stops accepting the requests to its queue, but the TimerThread as such is not stopped.



回答2:

Timer is single thread which holds list of Task to execute, once all Task are executed suucessfully the Timer thread instance will called by Garbage Collector after some time



回答3:

This might help you...The section describes the reasons in some cases the timer thread doesn't end.

http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html



标签: java timer