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?
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.