Java Timer thread hanging after main thread throws exception
843804Mar 18 2005 — edited Mar 18 2005My JAVA program runs a Timer thread and then throws an exception. However the program does not exit. Why is it so? How to solve this problem?
code snippet
public static void main(String[] args)
{
int numberOfMillisecondsInTheFuture = 9000;
Date timeToRun = new Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Ahoh i am here " );
}
}, timeToRun);
String s = null;
System.out.println(s.length()); // INTENTIONAL EXCEPTION
}
OUTPUT
Exception in thread "main" java.lang.NullPointerException
at a.main(a.java:27)
Ahoh i am here
hangs here
Why does the program hang? After timer expiry it is supposed to come out, is it not?