You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by zh...@apache.org on 2010/10/20 11:10:37 UTC

svn commit: r1024556 - /harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java

Author: zhoukevin
Date: Wed Oct 20 09:10:36 2010
New Revision: 1024556

URL: http://svn.apache.org/viewvc?rev=1024556&view=rev
Log:
This patch fixes a problem of which the thread of java.util.Timer should be closed when VM is off.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java?rev=1024556&r1=1024555&r2=1024556&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Timer.java Wed Oct 20 09:10:36 2010
@@ -18,6 +18,8 @@
 package java.util;
 
 import org.apache.harmony.luni.internal.nls.Messages;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 
 /**
  * {@code Timer}s are used to schedule jobs for execution in a background process. A
@@ -191,6 +193,11 @@ public class Timer {
          * @param name thread's name
          * @param isDaemon daemon thread or not
          */
+        TimerImpl(boolean isDaemon) {
+            this.setDaemon(isDaemon);
+            this.start();
+        }
+
         TimerImpl(String name, boolean isDaemon) {
             this.setName(name);
             this.setDaemon(isDaemon);
@@ -204,7 +211,7 @@ public class Timer {
         @Override
         public void run() {
             while (true) {
-                TimerTask task;
+                final TimerTask task;
                 synchronized (this) {
                     // need to check cancelled inside the synchronized block
                     if (cancelled) {
@@ -370,6 +377,18 @@ public class Timer {
     	}
         this.impl = new TimerImpl(name, isDaemon);
         this.finalizer = new FinalizerHelper(impl);
+        if (isDaemon) {
+            AccessController.doPrivileged(new PrivilegedAction<Object>() {
+                public Object run() {
+                    Runtime.getRuntime().addShutdownHook(new Thread() {
+                        public void run() {
+                            cancel();
+                        }
+                    });
+                    return null;
+                }
+            });
+        }
     }
     
     /**