Raghavan:
but now terminating a Callable in ThreadPool, just abruptly terminates at some point which we have no control of.[/QB]
I did not get this. It will not be *abruptly* terminated.
If you cancel the future, it will send an interrupt signal to the working thread.
If the thread is sleeping, blocked on interruptible I/O or waiting it will get the Interrupted exception. As this is a checked exception you will always have a try-catch corresponding to it.
If the task is working when it is cancelled, then following may happen:
If the task checks for interrupted flag, it can terminate itself i.e. return from run() If it calls sleep, wait or interruptible blocking I/O operations, it will immediately get an Interrupted exception. These methods check for the interrupted flag before performing any action. The task is running continuously and does not do any of the above. This will never stop the task. BTW,
If you want to log on completion of a task. You can use the
afterExecute()method of ThreadPoolExecutor. You have to extend this class and override the method to do anything specific after task completion.
P.S.: A true value returned from Future.cancel() does not indicate that the task is completed. It just means that if it has not yet started, it will not be executed now. If started and mayInterruptIfRunning is true, then it is sent an interrupt signal. Whether the task responds to interrupt is totally dependent on the task.
[ May 06, 2008: Message edited by: Nitesh Kant ]