class DaemonThread extends Thread {
public void run() {
System.out.println("Entering run method");
try {
System.out.println("In run Method: currentThread() is"
+ Thread.currentThread());
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException x) {
System.out.println("hi");
}
// System.out.println("In run method: woke up again");
finally {
System.out.println("Leaving run1 Method");
}
}
} finally {
System.out.println("Leaving run Method");
}
}
public static void main(String[] args) {
System.out.println("Entering main Method");
DaemonThread t = new DaemonThread();
t.setDaemon(true);
t.start();
try {
Thread.sleep(900);
} catch (InterruptedException x) {}
System.out.println("Leaving main method");
}
}
why second finally method not run…as i know finally method must have have to run whatever the condition is ..but in this case only first finally method, why not second finally run.
The
printlnstatement is never reached because of thewhile(true)loop that never ends!If you ever leaves that loop, then the second
finallyblock would be executed.