The moose likes Threads and Synchronization and the fly likes Kill a worker thread hung in IO Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Kill a worker thread hung in IO" Watch "Kill a worker thread hung in IO" New topic
Author

Kill a worker thread hung in IO

iarizona iarizona
Greenhorn

Joined: Nov 27, 2002
Posts: 3
When a worker thread is hung in a synchronous IO operation (such as a JDBC call) here is a trick I developed on how to kill the worker thread as well as the supervisory thread.
You may say "but this is brute force, not very elegant!"
Well, at least this brute solution works!
Try using interrupt() or stop() (deprecated, by the way). The worker thread does not respond at all...
So this is the only solution I found.
Enjoy!

/**
* Start thread
*/

public void run () {
cat.debug(Thread.currentThread().getName() + " thread started.");

if (Thread.currentThread().getName().equalsIgnoreCase("SUPERVISOR")) {
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {}

//Supervisor wokeup

if (queryInProgress) {
stopProcessing = true;
cat.debug(worker.thread.getName() + " will be aborted by supervisory monitor. Query exceeded threshold.");
Runtime.getRuntime().exit(1);
}
}
else {
//Worker thread
worker.sequenceProcessing();
}
}
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1158

I, on the other hand, close the IO channel from a seperate thread.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I, on the other hand, close the IO channel from a seperate thread.

A much wiser approach. Kinda like the difference between tapping somebody on the shoulder and hitting them in th face with a frying pan to get their attention.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Since the example given is a JDBC call, I think the preferred solution is to call cancel() on the Statement executing the call (assuming the driver supports it.) Same basic idea though.
Still, while the frying pan usually shouldn't be the first choice, it's worth remembering that the option is available, as it may be necessary sometime...


"I'm not back." - Bill Harding, Twister
 
 
subject: Kill a worker thread hung in IO
 
Threads others viewed
Issue with executor thread pool
Thread State
synchronize method call
How to handle/kill a hung thread
Thread Monitoring a Thread
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com