如何在定时器中终止 TimerThread [英] How to terminate TimerThread in Timer
问题描述
当我阅读java.util.Timer
的源代码时,我发现java.util.Timer
是由一个TimerThread
实现的和一个 TaskQueue
When I read the source code of java.util.Timer
, I found that java.util.Timer
is implemented by a TimerThread
and a TaskQueue
据说java.util.Timer
可以通过java.util.Timer.cancel()
取消,
但是当我调用 java.util.Timer.cancel()
时,我发现 Timer 中的 TimerThread 仍在运行,
But when I call java.util.Timer.cancel()
I found TimerThread in Timer still running ,
这是在线系统的问题吗?
Does this a problem for online system ?
如果可能的话,我怎样才能在 java.util.Timer
中停止 TimerThread
?
And if possible , how can I stop TimerThread
inside java.util.Timer
?
从 TimerThread
(jdk 7) 的来源我终于发现 Timer.cancel()
确实有效.
From source of TimerThread
(jdk 7) I finally found that Timer.cancel()
do works.
TimerThread wait TaskQueue
,当 Timer.cancel()
调用时,TaskQueue
notify TimerThread
, 如果`TaskQueue is EMPTY...
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.
推荐答案
定时器线程仍将运行.但是计时器线程将根据实现停止将任何内容接收到队列中.
Timer thread will still be running. But the timer thread will stop accepting anything into the queue as per the implementation.
timer
(TimerThread) 线程只能通过 Timer
类访问.TimerThread
封装在 Timer
类中.在实现方面,他们已确保 Timer 停止接受对其队列的请求,但 TimerThread
本身并未停止.
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.
这篇关于如何在定时器中终止 TimerThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!