Java: Allowing the child thread to kill itself on InterruptedException?
- by Zombies
I am using a ThreadPool via ExecutorService. By calling shutDownNow() it interrupts all running threads in the pool. When this happens I want these threads to give up their resources (socket and db connections) and simply die, but without continuing to run anymore logic, eg: inserting anything into the DB. What is the simplest way to achieve this? Bellow is some sample code:
public void threadTest() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(999999);
} catch (InterruptedException e) {
//invoke thread suicide logic here
}
}
});
t.start();
t.interrupt();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
}