Hi,
Ran into a problem. I have a spawned thread that must call its finally block. Assuming code below, is it possible to ensure that finally in the runnable is called?
import org.junit.Test;
public class ThreadTest {
@Test
public void testThreadFinally() throws InterruptedException {
Runnable r = new Runnable() {
@Override
public void run() {
try {
System.out.println("Going to sleep...");
Thread.sleep(1000);
System.out.println("slept 1000 ms");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("Must print this.");
}
}
};
Thread t = new Thread(r);
t.setDaemon(false);
t.start();
Thread.sleep(500);
System.out.println("main thread about to throw runtime...");
throw new RuntimeException("fail");
}
}