kuhu vora wrote:
Mike Simmons wrote:
kuhu vora wrote:
First, a minor point, I think you need to decide if you're using hours or minutes, and be consistent. At least, consistent within a given method call.
Second, I think this "cancel" task is preventing you from shutting down as promptly as you'd like. I assume it's set to stop everything sometime in the future. If you try to shutdown() before that, well, this cancellation task is already scheduled, so the service will wait until it executes. To shutdown more promptly, use shutdownNow() rather than shutdown(). The shutdownNow() method will not only prevent any new tasks from being schedules, but it will also remove any already-scheduled tasks that have not run yet, and it will attempt to interrupt any currently running tasks. That's probably what you want.
Last, even shutdownNow() merely starts the shutdown process. It doesn't wait until it's over. If you want to print "tests completed" after everything is shut down, you need to also call awaitTermination() after the shutdownNow().
Salil Wadnerkar wrote:Oh, you are right. I missed the last line. So, beeperHandle.cancel(true) cancels the periodic executions. You can just add scheduler.shutdown() after that statement and then print whatever you wanted to print.
Salil Wadnerkar wrote:The call that cancels the user scheduled tasks is not .shutdown() or .shutdownNow(), but, beeperHandle.reset().
Salil Wadnerkar wrote:From what you said, you don't want to cancel user's task, but execute some logic when their task is complete. For that, you can check it with beeperHandle.isDone() and if it is true, execute some logic like this:
Salil Wadnerkar wrote:Thanks Mike for the explanation. Yes, you are right that I meant cancel() on the beeperTask.
The original code called cancel() on the Future first and then shutdown() on the Executor. So, that's why I said that cancel() has already cancelled the tasks. (If cancel was not called, shutdown() would have cancelled these tasks).
After some googing, my proposed isDone() will not work (as a ScheduledFuture never gets to complete "normally") and there does not seem to any other way than forcefully canceling user tasks using cancel().