aspose file tools*
The moose likes Threads and Synchronization and the fly likes timed thread interrupt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "timed thread interrupt" Watch "timed thread interrupt" New topic
Author

timed thread interrupt

Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
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?


Darryl Failla
Sun Certified Java 2 Programmer
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
wood burning stoves
 
subject: timed thread interrupt