• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

stopping a runnable in ThreadPoolExecutor

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
     
    There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic