harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jessewil...@apache.org
Subject svn commit: r882385 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Timer.java test/api/common/org/apache/harmony/luni/tests/java/util/TimerTest.java
Date Fri, 20 Nov 2009 02:01:44 GMT
Author: jessewilson
Date: Fri Nov 20 02:01:43 2009
New Revision: 882385

URL: http://svn.apache.org/viewvc?rev=882385&view=rev
Log:
Changing timers so timer threads die if ever timer tasks throw unchecked exceptions. Previously
we kept the timer alive (not consistent with RI) and suppressed an exception (bad user experience).
With this change, once a timer task fails, its timer will no longer accept new tasks.

Modified:
    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/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=882385&r1=882384&r2=882385&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 Fri Nov
20 02:01:43 2009
@@ -217,7 +217,7 @@
                         // no tasks scheduled -- sleep until any task appear
                         try {
                             this.wait();
-                        } catch (InterruptedException e) {
+                        } catch (InterruptedException ignored) {
                         }
                         continue;
                     }
@@ -241,8 +241,7 @@
                         // sleep!
                         try {
                             this.wait(timeToSleep);
-                        } catch (InterruptedException e) {
-                            // Ignored
+                        } catch (InterruptedException ignored) {
                         }
                         continue;
                     }
@@ -285,11 +284,16 @@
                     }
                 }
 
-                // run the task
+                boolean taskCompletedNormally = false;
                 try {
                     task.run();
-                } catch (Exception e) {
-                    // Ignored
+                    taskCompletedNormally = true;
+                } finally {
+                    if (!taskCompletedNormally) {
+                        synchronized (this) {
+                            cancelled = true;
+                        }
+                    }
                 }
             }
         }

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=882385&r1=882384&r2=882385&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
Fri Nov 20 02:01:43 2009
@@ -20,6 +20,7 @@
 import java.util.Date;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.atomic.AtomicReference;
 
 public class TimerTest extends junit.framework.TestCase {
 
@@ -1181,6 +1182,25 @@
 		}
 	}
 
+    /**
+     * We used to swallow RuntimeExceptions thrown by tasks. Instead, we need to
+     * let those exceptions bubble up, where they will both notify the thread's
+     * uncaught exception handler and terminate the timer's thread.
+     */
+    public void testThrowingTaskKillsTimerThread() throws InterruptedException {
+        final AtomicReference<Thread> threadRef = new AtomicReference<Thread>();
+        new Timer().schedule(new TimerTask() {
+            @Override public void run() {
+                threadRef.set(Thread.currentThread());
+                throw new RuntimeException("task failure!");
+            }
+        }, 1);
+
+        Thread.sleep(400);
+        Thread timerThread = threadRef.get();
+        assertFalse(timerThread.isAlive());
+    }
+
 	protected void setUp() {
 		timerCounter = 0;
 	}



Mime
View raw message