You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/05 07:26:44 UTC
svn commit: r411672 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/Timer.java test/java/tests/api/java/util/TimerTest.java
Author: mloenko
Date: Sun Jun 4 22:26:43 2006
New Revision: 411672
URL: http://svn.apache.org/viewvc?rev=411672&view=rev
Log:
improvement from HARMONY-553
Java 5 Enhancement: java.util.Timer should implement purge() method, Timer(String), Timer(String, boolean) constructors
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/TimerTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java?rev=411672&r1=411671&r2=411672&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Timer.java Sun Jun 4 22:26:43 2006
@@ -52,9 +52,31 @@
public TimerNode(TimerTask value) {
this.task = value;
}
- }
+
+ public void deleteIfCancelled(TimerTree tasks) {
+
+ /*
+ * All changes in the tree structure during deleting this node
+ * affect only the structure of the subtree having this node as
+ * its root
+ */
+ if (left != null) {
+ left.deleteIfCancelled(tasks);
+ }
+ if (right != null) {
+ right.deleteIfCancelled(tasks);
+ }
+ if (task.isCancelled()) {
+ tasks.delete(this);
+ tasks.deletedCancelledNumber++;
+ }
+ }
+ }
private static final class TimerTree {
+
+ int deletedCancelledNumber;
+
TimerNode root;
boolean isEmpty() {
@@ -150,6 +172,12 @@
this.start();
}
+ TimerImpl(String name, boolean isDaemon) {
+ this.setName(name);
+ this.setDaemon(isDaemon);
+ this.start();
+ }
+
/**
* This method will be launched on separate thread for each Timer
* object.
@@ -242,7 +270,14 @@
tasks = new TimerTree();
this.notify();
}
- }
+
+ public int purge() {
+ // callers are synchronized
+ tasks.deletedCancelledNumber = 0;
+ tasks.root.deleteIfCancelled(tasks);
+ return tasks.deletedCancelledNumber;
+ }
+ }
/* This object will be used in synchronization purposes */
private TimerImpl impl;
@@ -274,14 +309,28 @@
impl = new TimerImpl(false);
}
+ public Timer(String name, boolean isDaemon) {
+ impl = new TimerImpl(name, isDaemon);
+ }
+
+ public Timer(String name) {
+ impl = new TimerImpl(name, false);
+ }
+
/**
- * Cancels the Timer and removed any scheduled tasks. If there is a
- * currently running task it is not effected. No more tasks may be scheduled
- * on this Timer. Subsequent calls do nothing.
- */
+ * Cancels the Timer and removed any scheduled tasks. If there is a
+ * currently running task it is not effected. No more tasks may be scheduled
+ * on this Timer. Subsequent calls do nothing.
+ */
public void cancel() {
impl.cancel();
}
+
+ public int purge() {
+ synchronized (impl) {
+ return impl.purge();
+ }
+ }
/**
* Schedule a task for single execution. If when is less than the current
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/TimerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/TimerTest.java?rev=411672&r1=411671&r2=411672&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/TimerTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/TimerTest.java Sun Jun 4 22:26:43 2006
@@ -140,8 +140,56 @@
}
/**
- * @tests java.util.Timer#cancel()
- */
+ * @tests java.util.Timer#Timer(String, boolean)
+ */
+ public void test_ConstructorSZ() {
+ Timer t = null;
+ try {
+ // Ensure a task is run
+ t = new Timer("test_ConstructorSZThread", true);
+ TimerTestTask testTask = new TimerTestTask();
+ t.schedule(testTask, 200);
+ synchronized (sync) {
+ try {
+ sync.wait(1000);
+ } catch (InterruptedException e) {}
+ }
+ assertEquals("TimerTask.run() method not called after 200ms", 1,
+ testTask.wasRun());
+ t.cancel();
+ } finally {
+ if (t != null)
+ t.cancel();
+ }
+ }
+
+ /**
+ * @tests java.util.Timer#Timer(String)
+ */
+ public void test_ConstructorS() {
+ Timer t = null;
+ try {
+ // Ensure a task is run
+ t = new Timer("test_ConstructorSThread");
+ TimerTestTask testTask = new TimerTestTask();
+ t.schedule(testTask, 200);
+ synchronized (sync) {
+ try {
+ sync.wait(1000);
+ } catch (InterruptedException e) {}
+ }
+ assertEquals("TimerTask.run() method not called after 200ms", 1,
+ testTask.wasRun());
+ t.cancel();
+ } finally {
+ if (t != null)
+ t.cancel();
+ }
+ }
+
+ /**
+ * @tests java.util.Timer#cancel()
+ */
public void test_cancel() {
Timer t = null;
try {
@@ -238,8 +286,40 @@
}
/**
- * @tests java.util.Timer#schedule(java.util.TimerTask, java.util.Date)
- */
+ * @tests java.util.Timer#purge()
+ */
+ public void test_purge() throws Exception {
+ Timer t = null;
+ try {
+ t = new Timer();
+ TimerTestTask[] tasks = new TimerTestTask[100];
+ int[] delayTime = { 50, 80, 20, 70, 40, 10, 90, 30, 60 };
+
+ int j = 0;
+ for (int i = 0; i < 100; i++) {
+ tasks[i] = new TimerTestTask();
+ t.schedule(tasks[i], delayTime[j++], 200);
+ if (j == 9) {
+ j = 0;
+ }
+ }
+
+ for (int i = 0; i < 50; i++) {
+ tasks[i].cancel();
+ }
+
+ assertTrue(t.purge() <= 50);
+ assertEquals(t.purge(), 0);
+ } finally {
+ if (t != null) {
+ t.cancel();
+ }
+ }
+ }
+
+ /**
+ * @tests java.util.Timer#schedule(java.util.TimerTask, java.util.Date)
+ */
public void test_scheduleLjava_util_TimerTaskLjava_util_Date() {
Timer t = null;
try {