How to stop immediately the task scheduled in Java.util.Timer class

不问归期 提交于 2019-11-29 07:37:47

There is no way for the Timer to stop the task in its tracks.

You will need to have a separate mechanism in the running task itself, that checks if it should keep running. You could for instance have an AtomicBoolean keepRunning variable which you set to false when you want the task to terminate.

if your timer is using some sort of file/socket etc, you can close that object from outside the timer, and the timer task will throw an exception, and you can use it to stop the timer.

but in general you need some sort of poison pill to successfully stop a separate thread/timer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!