let us say i have a runnable to execute and i want the ability to stop in the middle.
i execute the runnable using
inside the search runnable, there is code to wait() and look for any interrupts implementing like below..
I am not sure how to stop this Runnable. remove() on the thread pool using a Runnable only removes it from the queue. The shutDownNow() stops Runnable successfully but that does not end gracefully as I wanted to handle interruptions.
I did find a way to stop threads in this approach but not sure when it runs in ThreadPool.
If instead of execute method in ThreadPoolExecutor, you use submit, it will give you a Future back. You can call cancel on the returned Future to stop your task. [ May 02, 2008: Message edited by: Nitesh Kant ]
Raghavan: i hope this is the standard approach to stop a thread when run using a pool executor.
Well this is a suggested way of cancelling a submitted task. While using a threadpool, one should not be concerned about the threads that are executing the tasks. The lifecycle of the threads are totally controlled by the threadpool. There is no such standard involved but yeah this is how a Future is supposed to be cancelled and it is completely legal.
Joined: Dec 20, 2005
sorry, i did actually mean stopping a Callable running in one of the threads in the ThreadPool.
Suppose if we want the Callable task to perform some log operations when it is terminated, is it possible to accomplish that? When interrupting a thread stopping not running in a ThreadPool in sleep mode, it throws InterruptedException and we could do log operations inside the catch block of InterruptedException. Also we had more control by invoking sleep() only at the point we needed. but now terminating a Callable in ThreadPool, just abruptly terminates at some point which we have no control of.
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 ]