wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes stopping a runnable in ThreadPoolExecutor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "stopping a runnable in ThreadPoolExecutor" Watch "stopping a runnable in ThreadPoolExecutor" New topic
Author

stopping a runnable in ThreadPoolExecutor

Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
hello,

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.


Summarizing: My question is how to stop a Runnable when running in ThreadPoolExecutor?

thank you

[ May 02, 2008: Message edited by: Raghavan Chockalingam ]
[ May 02, 2008: Message edited by: Raghavan Chockalingam ]

Raghavan
SCJP 6
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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 ]

apigee, a better way to API!
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
the solution suggested works. thanks very much. i hope this is the standard approach to stop a thread when run using a pool executor.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
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.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: stopping a runnable in ThreadPoolExecutor
     
    Similar Threads
    Producer Consumer Controller
    Single Producer and multiple consumer using Mulithreading
    synchronized thread???
    Difference between multithreading with and without Executor
    running a fixed number of threads all the time