Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Timer thread hanging after main thread throws exception

843804Mar 18 2005 — edited Mar 18 2005
My 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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 15 2005
Added on Mar 18 2005
1 comment
522 views