I have an application which uses a third party jar to do some task. My application uses Spring Executor pool to spawn the threads.
Question: I need to stop the current execution of a thread if the executing time exceeds the time limit
What I did: I googled a lot about interrupting thread. Most of says to check the interrupt flag whether the thread is interrupted or not and if interrupted throw an interrupted exception. But in thread I'm calling a method which is in a third party jar which do most of the stuffs so I can't go there and change the code for checking the interrupt flag.
Consider the following code snippet:
public void run() {
boolean isDone = false;
isDone = callThirdPartyMethod(); // here for some inputs thread takes more time (even never return for an hour!!) to process which I don't want and I need to stop/interrupt the thread
}
Approaches I follow for stopping/interrupting it
- I had wrote a TimerTask which checks for the thread which is exceeding the time limit and call the interrupt() on them (but it seems nothing happens as they continue their execution).
- I changed the code of my TimerTask from interrupting it to calling the stop() on thread. This works but it throws ThreadDeath error. Also what I read in most of the blogs and even in stackoverflow is that we should never call the stop method.