Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

timed thread interrupt

 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an intermittent problem with stuck threads due to network problems. What I would like to do is start and stop a timer at the start and end of my thread and have an exception be thrown if the timer runs out (thread got stuck). The exception would get me out of my thread then I could just reset and restart it. The run() override in TimerTask can't throw an exception so I can't use that. Anyone come across options for this?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwing an exception from a timer thread won't stop the frozen task - it will come from a different thread. So the first thing you have to do is read the API for the class/library you are using for network communication. First check if there are alternative forms of the calls that have built-in timeouts and use those if possible. If not, then you will have to learn what you can do to cause an exception that actually breaks the task that is stuck (for example, closing a connection, or something). Then your timer task would wait, and at the end of the wait do the thing that breaks the task. Of course, you will have to handle the task being broken.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic