• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stopping a thread in threadPoolExecutor

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
We have some code in place already using ThreadPoolExecutor where each thread takes care of tasks like copying files from one machine to another, performing checks and so on. Need a way to stop a thread based on a condition. The internet did yield some posts saying it's doable by using Future. Since the actual code cannot be posted, the below code simulates what we need but it appears that calling Future.cancel(true) doesn't cancel the thread and calling shutdown() doesn't really close down the pool. Please take a look at the below to see if there's anything else needed or a flaw in the code. Appreciate your time!

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Parimala Ramdas wrote:it appears that calling Future.cancel(true) doesn't cancel the thread


This call does cancel the submitted task, not the thread.
In your program only task number 4 is cancelled ...take a thorough look at the output, the task no. 4 doesn't appear in the output
- it is canceled so it even does not start:


Parimala Ramdas wrote: and calling shutdown() doesn't really close down the pool.


Shutdown doesn't stop/cancel submitted tasks - after shutdown() is called, no new tasks can be submitted to the pool,
but the pool continues executing already submitted tasks.
If you want to stop tasks that are submitted/executed by the pool, you must call shutdownNow.
Consult api docs for details:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
 
Parimala Ramdas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Kordal. shutdownNow() worked with the original code.

We changed it a little to simulate the IO operations that our application has to perform and it doesn't stop the loop.

Our requirement is to cancel a task that has already started (like an IO task that checks out files from a repository and copies them to a different machine). If shutdownNow() is unable to stop a loop from executing (as seen below) we are doubtful whether it will stop an IO operation....


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here is your problem. In your loop you have:


You catch an interruption and you throw it away. The only way to safely 'stop' a thread is to interrupt it. So shutdownNow, and other cancel type methods will interrupt the intended threads. You catch that interruption and ignore it, so yeah, your loop keeps going. What you need to do is stop the loop when the thread gets interrupted.

For your IO task - your task has to respond to interruption as well. If it doesn't then you have to detect the interrupt and see if there is something else you can do to make the task end (such as forceably close the Stream, or cause some other exception to occur). But to respond to shutdownNow and cancel you MUST be able to respond to thread interrupt.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic