hs-comp: VM is not exiting

David Holmes david.holmes at oracle.com
Fri Jun 12 01:37:18 UTC 2015


On 12/06/2015 3:12 AM, Andrew Haley wrote:
> On 06/11/2015 06:11 PM, Vitaly Davidovich wrote:
>> The OS is waiting.
>>
>> This class is fairly bad, and should be avoided because the internal thread
>> is (a) non-daemon and (b) cannot be interrupted (it swallows IE and
>> continues).  Javadoc calls this out:
>>
>> 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 timer's
>> cancel method.
>
> But the Timer should time out after 1 second...

The timer fires as expected, but the VM won't terminate unless the timer 
is cancelled:

import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {

     static volatile boolean finished = true;
     static Timer timer = new Timer();
     static boolean cancel = false;
     public static void main(String[] args) {
         if (args.length >0)
             cancel = true;

         TimerTask t = new TimerTask() {
             public void run() {
                 finished = true;
                 System.out.println("Timer fired");
                 if (cancel) timer.cancel();
             }
         };
         timer.schedule(t, 1000);
     }
}

 > java TimerTest
Timer fired
^C
 > java TimerTest cancel
Timer fired
 >

David
-----


> Andrew.
>


More information about the hotspot-dev mailing list