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

前端 未结 2 587
渐次进展
渐次进展 2020-12-18 06:12

I tried everything. This one too How to stop the task scheduled in Java.util.Timer class

I have one task that implements java.util.TimerTask

I call that t

相关标签:
2条回答
  • 2020-12-18 06:42

    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.

    0 讨论(0)
  • 2020-12-18 06:43

    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.

    0 讨论(0)
提交回复
热议问题