Pitfall - Catching InterruptedException
suggest changeAs already pointed out in other pitfalls, catching all exceptions by using
try {
// Some code
} catch (Exception ex) {
// Some error handling
}
Comes with a lot of different problems. But one particular problem is that it can lead to deadlocks as it breaks the interrupt system when writing multi-threaded applications.
If you start a thread you usually also need to be able to stop it abruptly for various reasons.
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
// Do something indefinitely
}
}
});
t.start();
//Do something else
// The thread should be canceled if it is still active.
// A better way to solve this is with a shared variable that is tested
// regularly by the thread for a clean exit, but for this example we try to
// forcibly interrupt this thread.
if (t.isAlive()) {
t.interrupt();
t.join();
}
//Continue with program
t.interrupt() sets that thread's interrupt status. It does not throw InterruptedException by itself. Blocking calls such as sleep, wait, and join throw that exception when the thread is interrupted. A loop that never makes such a call keeps running. If the thread does get InterruptedException, it can catch it and clean up before it is completely stopped.
Thread t = new Thread(new Runnable() {
public void run() {
try {
while (true) {
// Do something indefinitely
}
} catch (InterruptedException ex) {
//Do some quick cleanup
// In this case a simple return would do.
// But if you are not 100% sure that the thread ends after
// catching the InterruptedException you will need to raise another
// one for the layers surrounding this code.
Thread.currentThread().interrupt();
}
}
});
But if you have a catch-all expression in your code, the InterruptedException will be caught by it as well and the interruption will not continue. Which in this case could lead to a deadlock as the parent thread waits indefinitely for this thread to stop with t.join().
Thread t = new Thread(new Runnable() {
public void run() {
try {
while (true) {
try {
// Do something indefinitely
}
catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (InterruptedException ex) {
// Dead code as the interrupt exception was already caught in
// the inner try-catch
Thread.currentThread().interrupt();
}
}
});
So it is better to catch Exceptions individually, but if you insist on using a catch-all, at least catch the InterruptedException individually beforehand.
Thread t = new Thread(new Runnable() {
public void run() {
try {
while (true) {
try {
// Do something indefinitely
} catch (InterruptedException ex) {
throw ex; //Send it up in the chain
} catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (InterruptedException ex) {
// Some quick cleanup code
Thread.currentThread().interrupt();
}
}
});