• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

timed thread interrupt

Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
Posts: 4179
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic