harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r796142 - in /harmony/enhanced/classlib/trunk/modules/luni: .classpath src/main/java/java/util/Timer.java src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java
Date Tue, 21 Jul 2009 04:49:37 GMT
Author: ndbeyer
Date: Tue Jul 21 04:49:37 2009
New Revision: 796142

URL: http://svn.apache.org/viewvc?rev=796142&view=rev
Log:
Add generated Timer Thread name, Apply modified version of HARMONY-6277 - Timer's finalizer
throws an NPE if the Timer wasn't constructed properly

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/.classpath
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/.classpath
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/.classpath?rev=796142&r1=796141&r2=796142&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/.classpath (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/.classpath Tue Jul 21 04:49:37 2009
@@ -14,5 +14,6 @@
 		</accessrules>
 	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+	<classpathentry kind="lib" path="/home/nathan/harmony/working_classlib/deploy/jdk/jre/lib/boot/luni-kernel-stubs.jar"/>
 	<classpathentry kind="output" path="bin/main"/>
 </classpath>

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java?rev=796142&r1=796141&r2=796142&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java Tue Jul
21 04:49:37 2009
@@ -187,14 +187,10 @@
 
         /**
          * Starts a new timer.
-         *
-         * @param isDaemon
+         * 
+         * @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);
@@ -324,60 +320,78 @@
         }
 
     }
+    
+	private static final class FinalizerHelper {
+		private final TimerImpl impl;
+		
+		FinalizerHelper(TimerImpl impl) {
+			super();
+			this.impl = impl;
+		}
+		
+		@Override
+		protected void finalize() {
+			synchronized (impl) {
+				impl.finished = true;
+				impl.notify();
+			}
+		}
+	}
+	
+	private static long timerId;
+	
+	private synchronized static long nextId() {
+		return timerId++;
+	}
 
     /* This object will be used in synchronization purposes */
-    private TimerImpl impl;
+    private final TimerImpl impl;
 
     // Used to finalize thread
     @SuppressWarnings("unused")
-    private Object finalizer = new Object() { // $NON-LOCK-1$
-        @Override
-        protected void finalize() {
-            synchronized (impl) {
-                impl.finished = true;
-                impl.notify();
-            }
-        }
-    };
+    private final FinalizerHelper finalizer;
 
     /**
-     * Creates a new {@code Timer} which may be specified to be run as a daemon thread.
+     * Creates a new named {@code Timer} which may be specified to be run as a
+     * daemon thread.
      *
-     * @param isDaemon
-     *            {@code true} if the {@code Timer}'s thread should be a daemon thread.
+     * @param name the name of the {@code Timer}.
+     * @param isDaemon true if {@code Timer}'s thread should be a daemon thread.
+     * @throws NullPointerException is {@code name} is {@code null}
      */
-    public Timer(boolean isDaemon) {
-        impl = new TimerImpl(isDaemon);
+    public Timer(String name, boolean isDaemon) {
+    	super();
+    	if (name == null){
+    		throw new NullPointerException("name is null");
+    	}
+        this.impl = new TimerImpl(name, isDaemon);
+        this.finalizer = new FinalizerHelper(impl);
     }
-
+    
     /**
-     * Creates a new non-daemon {@code Timer}.
+     * Creates a new named {@code Timer} which does not run as a daemon thread.
+     *
+     * @param name the name of the Timer.
+     * @throws NullPointerException is {@code name} is {@code null}
      */
-    public Timer() {
-        impl = new TimerImpl(false);
+    public Timer(String name) {
+        this(name, false);
     }
-
+    
     /**
-     * Creates a new named {@code Timer} which may be specified to be run as a
-     * daemon thread.
+     * Creates a new {@code Timer} which may be specified to be run as a daemon thread.
      *
-     * @param name
-     *            the name of the {@code Timer}.
-     * @param isDaemon
-     *            true if {@code Timer}'s thread should be a daemon thread.
+     * @param isDaemon {@code true} if the {@code Timer}'s thread should be a daemon thread.
      */
-    public Timer(String name, boolean isDaemon) {
-        impl = new TimerImpl(name, isDaemon);
+    public Timer(boolean isDaemon) {
+        this("Timer-" + Timer.nextId(), isDaemon);
     }
 
     /**
-     * Creates a new named {@code Timer} which does not run as a daemon thread.
-     *
-     * @param name
-     *            the name of the Timer.
+     * Creates a new non-daemon {@code Timer}.
      */
-    public Timer(String name) {
-        impl = new TimerImpl(name, false);
+    public Timer() {
+        this(false);
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java?rev=796142&r1=796141&r2=796142&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java
Tue Jul 21 04:49:37 2009
@@ -189,6 +189,22 @@
         }
     }
 
+    public void testConstructorThrowsException() {
+        try {
+            new Timer(null, true);
+            fail("NullPointerException expected");
+        } catch (NullPointerException e) {
+            //expected
+        }
+
+        try {
+            new Timer(null, false);
+            fail("NullPointerException expected");
+        } catch (NullPointerException e) {
+            //expected
+        }
+    }
+
     /**
      * @tests java.util.Timer#cancel()
      */



Mime
View raw message