I use java.util.Timer to schedule a periodic task. At some point, I would like to close it and wait for it to finish .
Timer.cancel () will prevent any future tasks from starting. How can I make sure that any tasks are not currently running (or wait for them, if any?)
I can imagine external synchronization mechanisms, but I donβt see how they can cover all cases. For example, if I synchronize on some monitor inside the task, I will still miss the case when the task just started, but did not take the monitor.
What is the recommended practice for waiting for all tasks to be completed, including currently running tasks?
java synchronization timer monitor
ripper234
source share