I'm using java.util.Timer to schedule a periodic task. At one point, I'd like to shut it down, and wait for it to finish.
Timer.cancel() will prevent any future tasks from running. How do I make sure any tasks are not running at the moment (or wait for them if they are?)
I can introduce external synchronization mechanisms, but I don't see how they can cover all cases. For example, if I synchronize on some Monitor within the task, I still miss the case when the task just started executing but didn't take the monitor.
What is the recommended practice for waiting until all tasks are really done, including currently running tasks?
Timer
for some reason, then you can use a Condition (java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/…) to synchronize two threads on an event (such as completion of the scheduled task). – Clung