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

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?
 
Marshal
Posts: 28003
94
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
Marshal
Posts: 28003
94
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.
 
It means our mission is in jeapordy! Quick, read 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