• Post Reply Bookmark Topic Watch Topic
  • New Topic

Kill a worker thread hung in IO

 
iarizona iarizona
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I, on the other hand, close the IO channel from a seperate thread.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!