1.1 --- a/rt/emul/compact/src/main/java/java/util/Timer.java Sat Nov 02 16:40:37 2013 +0100
1.2 +++ b/rt/emul/compact/src/main/java/java/util/Timer.java Sat Nov 02 21:09:52 2013 +0100
1.3 @@ -26,6 +26,7 @@
1.4 package java.util;
1.5 import java.util.Date;
1.6 import java.util.concurrent.atomic.AtomicInteger;
1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.8
1.9 /**
1.10 * A facility for threads to schedule tasks for future execution in a
1.11 @@ -111,7 +112,7 @@
1.12 protected void finalize() throws Throwable {
1.13 synchronized(queue) {
1.14 thread.newTasksMayBeScheduled = false;
1.15 - queue.notify(); // In case queue is empty.
1.16 + thread.notifyQueue(1); // In case queue is empty.
1.17 }
1.18 }
1.19 };
1.20 @@ -156,8 +157,6 @@
1.21 * @since 1.5
1.22 */
1.23 public Timer(String name) {
1.24 - thread.setName(name);
1.25 - thread.start();
1.26 }
1.27
1.28 /**
1.29 @@ -171,9 +170,6 @@
1.30 * @since 1.5
1.31 */
1.32 public Timer(String name, boolean isDaemon) {
1.33 - thread.setName(name);
1.34 - thread.setDaemon(isDaemon);
1.35 - thread.start();
1.36 }
1.37
1.38 /**
1.39 @@ -407,7 +403,7 @@
1.40
1.41 queue.add(task);
1.42 if (queue.getMin() == task)
1.43 - queue.notify();
1.44 + thread.notifyQueue(1);
1.45 }
1.46 }
1.47
1.48 @@ -429,10 +425,10 @@
1.49 synchronized(queue) {
1.50 thread.newTasksMayBeScheduled = false;
1.51 queue.clear();
1.52 - queue.notify(); // In case queue was already empty.
1.53 + thread.notifyQueue(1); // In case queue was already empty.
1.54 }
1.55 }
1.56 -
1.57 +
1.58 /**
1.59 * Removes all cancelled tasks from this timer's task queue. <i>Calling
1.60 * this method has no effect on the behavior of the timer</i>, but
1.61 @@ -478,7 +474,7 @@
1.62 * reschedules repeating tasks, and removes cancelled tasks and spent
1.63 * non-repeating tasks from the queue.
1.64 */
1.65 -class TimerThread extends Thread {
1.66 +class TimerThread implements Runnable {
1.67 /**
1.68 * This flag is set to false by the reaper to inform us that there
1.69 * are no more live references to our Timer object. Once this flag
1.70 @@ -500,30 +496,41 @@
1.71 this.queue = queue;
1.72 }
1.73
1.74 + void notifyQueue(int delay) {
1.75 + if (delay < 1) {
1.76 + delay = 1;
1.77 + }
1.78 + setTimeout(delay, this);
1.79 + }
1.80 +
1.81 + @JavaScriptBody(args = { "delay", "r" }, body = "window.setTimeout(function() { r.run__V(); }, delay);")
1.82 + private static native void setTimeout(int delay, Runnable r);
1.83 +
1.84 public void run() {
1.85 - try {
1.86 - mainLoop();
1.87 - } finally {
1.88 - // Someone killed this Thread, behave as if Timer cancelled
1.89 - synchronized(queue) {
1.90 - newTasksMayBeScheduled = false;
1.91 - queue.clear(); // Eliminate obsolete references
1.92 - }
1.93 - }
1.94 + mainLoop(1);
1.95 +// try {
1.96 +// mainLoop(0);
1.97 +// } finally {
1.98 +// // Someone killed this Thread, behave as if Timer cancelled
1.99 +// synchronized(queue) {
1.100 +// newTasksMayBeScheduled = false;
1.101 +// queue.clear(); // Eliminate obsolete references
1.102 +// }
1.103 +// }
1.104 }
1.105
1.106 /**
1.107 * The main timer loop. (See class comment.)
1.108 */
1.109 - private void mainLoop() {
1.110 - while (true) {
1.111 + private void mainLoop(int inc) {
1.112 + for (int i = 0; i < 1; i += inc) {
1.113 try {
1.114 TimerTask task;
1.115 boolean taskFired;
1.116 synchronized(queue) {
1.117 // Wait for queue to become non-empty
1.118 while (queue.isEmpty() && newTasksMayBeScheduled)
1.119 - queue.wait();
1.120 + break;
1.121 if (queue.isEmpty())
1.122 break; // Queue is empty and will forever remain; die
1.123
1.124 @@ -548,12 +555,16 @@
1.125 }
1.126 }
1.127 }
1.128 - if (!taskFired) // Task hasn't yet fired; wait
1.129 - queue.wait(executionTime - currentTime);
1.130 + if (!taskFired) {
1.131 + // Task hasn't yet fired; wait
1.132 + notifyQueue((int)(executionTime - currentTime));
1.133 + return;
1.134 + }
1.135 }
1.136 if (taskFired) // Task fired; run it, holding no locks
1.137 task.run();
1.138 - } catch(InterruptedException e) {
1.139 + } catch(Exception e) {
1.140 + e.printStackTrace();
1.141 }
1.142 }
1.143 }