• 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

Stop a thread in ThreadPoolExecutor

 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like the Subject, is it possible to stop a running thread in the TheadPoolExecutor (JDK1.5)?

The shutdownNow() in ThreadPoolExecution stops all the running threads in the pool, but I want to stop a specific thread as requested.

I am thinking of maintaining a HashMap, where key=id, value=object reference of the thread being added into the ThreadPoolExecutor. Whenever I want to stop a particular thread, I find the object reference of the thread, then invoke a interrept() method.
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't stop the threads of the ThreadPoolExecutor, only its jobs. When you submit() a job (Callable or Runnable) the executor returns a Future. That has a method for canceling it.
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You shouldn't stop the threads of the ThreadPoolExecutor, only its jobs. When you submit() a job (Callable or Runnable) the executor returns a Future. That has a method for canceling it.



One problem I found with the submit() method is that the submit() method does not execute the thread itself. It relies on using the get() method on the Future object to "start the new thread" and the get() method makes the calling thread to go into a block (waiting) state until the get() method completes. In other words, if I invoke get() from the main thread, the main thread will be blocked. Any workaround? Thanks
 
Master Rancher
Posts: 4932
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jiafan Zhou wrote:One problem I found with the submit() method is that the submit() method does not execute the thread itself. It relies on using the get() method on the Future object to "start the new thread"


I don't think this is accurate. What makes you think it's the case?

Jiafan Zhou wrote:and the get() method makes the calling thread to go into a block (waiting) state until the get() method completes. In other words, if I invoke get() from the main thread, the main thread will be blocked. Any workaround? Thanks


This is true, if the associated worker thread hasn't completed yet. However it certainly should have been able to start without waiting for a get() method.

As for workarounds, I guess it really depends on what sort of workflow you envision. Is there something else that the main thread could be doing after all the tasks have been started? If so, don't call get() , but instead, do that other thing. I'm not sure there's a good general answer here, without knowing more about what the tasks are you're executing, and what (if anything) you want to do with the results when each task completes.
 
Rob Spoor
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Submitting will cause the job to start immediately. Just check out this little test:
Output:
Sleeping
Called
Woke up
Result: Called
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Jiafan Zhou wrote:One problem I found with the submit() method is that the submit() method does not execute the thread itself. It relies on using the get() method on the Future object to "start the new thread"


I don't think this is accurate. What makes you think it's the case?



I was thinking wrong on this. You are right, I have tested the submit which does *not* rely on get() to execute the job. Thanks.
I am not sure why I thought it was the case, it is probably because I am new to the java5 threading framework and completely being blink.

I encountered another problem in ThreadPoolExecutor at the following link:
https://coderanch.com/t/535248/threads/java/ThreadPoolExecutor-restart#2427797

Please shared your ideas on that thread as well.

Great thanks for the helps.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic