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

Issue with executor thread pool

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

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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 28327
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not safe at all. Read the API documentation for java.lang.Thread, in particular the documentation for the stop() method.
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28327
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic