views:

39

answers:

1

I have a question about the behaviour of Timer class in Java. This is the code: http://pastebin.com/mqcL9b1n

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        m.foo();
        m = null;
    }

    public void foo()
    {
        Timer t = new Timer();
        t.schedule(new SysPrint(), 200);
    }

}

class SysPrint extends TimerTask
{
    public void run()
    {
        System.out.println("Yes!");
    }
}

What happens is that if you run that program, it will print "Yes!" and it's not gonna do anything else (the program doesn't ends).

The Java documentation say: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection).

As I see this thing, the "last live reference" to the Timer object is gone after the 'foo()' functions ends. And the only task scheduled was the "Yes!" task that was executed, so I guess that after the process printed "Yes!", the Timer object should end and the process should terminate.

What happened here?

+2  A: 

Java is not exiting because your thread running the Timer is still kicking around. You have to mark that thread as being a daemon thread before Java will exit. You probably don't have access to the thread itself so unless Timer has a method to mark it so you'll have a hard time doing that. You'll need to manually stop it in a finally clause.

try {
   timer = new Timer();
   timer.schedule( new SysPrint(), 200 );
} finally {
   timer.cancel();
}
chubbard
I think that's not an answer to my question. That even never runs the "Yes!" print maybe. It schedule it, and then cancel the thread.
makakko
@makakko: instead of calling the `cancel()` method in `main()`, call the `TimerTask.cancel()` method from within your `SysPrint.run()` method.
Michael Burr
as explained in the javavdoc,you need to call cancel after running your task:"After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the the timer's cancel method."
crafty
Thanks all for the answers. It is pretty weird this behaviour.
makakko