Java: Allowing the child thread to kill itself on InterruptedException?
Posted
by Zombies
on Stack Overflow
See other posts from Stack Overflow
or by Zombies
Published on 2010-04-15T13:29:18Z
Indexed on
2010/04/15
13:33 UTC
Read the original article
Hit count: 206
java
|multithreading
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) {
}
}
© Stack Overflow or respective owner