my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Issue with executor thread pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Issue with executor thread pool" Watch "Issue with executor thread pool" New topic
Author

Issue with executor thread pool

sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156

Hi All,

I am using following code to create a thread pool using java executor framework.




the problem here is i need a way to forcefully clean all the threads spawned by executor thread pool. I understand that shutdownNow() will not work because thread is still actively running, but i wanted to get rid of thread if it doesn't return in some specified time.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
Ok, found a solution but don't know how good this is -

I have put responsibility on worker thread to check if it has got interrupted or not. This will make sure that if shutdownNow() is called which eventually interrupt the thread then work will stop working.


This works fine each time i run but to my surprise if i run in debug mode i can see wearied behavior. Some thread always left as in running mode even though log "is going to die" has got printed for all the running threads.

Any thoughts? How safe is it to implement this code?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Not safe at all. Read the API documentation for java.lang.Thread, in particular the documentation for the stop() method.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
Ya paul i know it's not safe and due to multiple complications with Stop method this was deprecated but what could be the alternative then? I have to forcefully kill thread in Executor thread pool incase they do not stop processing in fixed time.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You might want to consider reading this article. By the way it was high up in the list of links returned by Google when I used the keywords "java thread stop". You'll find other similar articles if you use those keywords; I suggest you read some of those as well.
 
Consider Paul's rocket mass heater.
 
subject: Issue with executor thread pool
 
Similar Threads
Number of working threads using java executor framework
autoterminating service
Threads behave in sequential Style
ExecutorService and FutureTask - Blocked Thread
Thread (maintain current thread)