File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Stop a thread in ThreadPoolExecutor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Stop a thread in ThreadPoolExecutor" Watch "Stop a thread in ThreadPoolExecutor" New topic
Author

Stop a thread in ThreadPoolExecutor

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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.


SCJP, SCJD, SCWCD, SCBCD, SCEA
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Oct 27, 2005
Posts: 19755
    
  20

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

Joined: Sep 28, 2005
Posts: 192

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:
http://www.coderanch.com/t/535248/threads/java/ThreadPoolExecutor-restart#2427797

Please shared your ideas on that thread as well.

Great thanks for the helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stop a thread in ThreadPoolExecutor