1.1 --- a/rt/emul/compact/src/main/java/java/util/Timer.java Wed Apr 27 04:01:39 2016 +0200
1.2 +++ b/rt/emul/compact/src/main/java/java/util/Timer.java Wed Apr 27 05:49:41 2016 +0200
1.3 @@ -490,7 +490,8 @@
1.4 * Otherwise, the Timer would never be garbage-collected and this
1.5 * thread would never go away.
1.6 */
1.7 - private TaskQueue queue;
1.8 + private final TaskQueue queue;
1.9 + private Object prevTimeout;
1.10
1.11 TimerThread(TaskQueue queue) {
1.12 this.queue = queue;
1.13 @@ -500,14 +501,41 @@
1.14 if (delay < 1) {
1.15 delay = 1;
1.16 }
1.17 - setTimeout(delay, this);
1.18 + prevTimeout = setTimeout(delay, this, prevTimeout);
1.19 }
1.20
1.21 - @JavaScriptBody(args = { "delay", "r" }, body = "window.setTimeout(function() { r.run__V(); }, delay);")
1.22 - private static native void setTimeout(int delay, Runnable r);
1.23 + @JavaScriptBody(args = { "delay", "r", "prev" }, body = ""
1.24 +// + "console.log('clear prev ' + prev);\n"
1.25 + + "if (prev) {\n"
1.26 + + " window.clearTimeout(prev);\n"
1.27 + + "}\n"
1.28 +// + "console.log('schedule in ' + delay);\n"
1.29 + + "return window.setTimeout(function() {\n"
1.30 +// + " console.log('running time');\n"
1.31 + + " r.run__V();\n"
1.32 +// + " console.log('done running time');\n"
1.33 + + "}, delay);\n"
1.34 + )
1.35 + private static native Object setTimeout(int delay, Runnable r, Object prev);
1.36 +
1.37 +// @JavaScriptBody(args = { "msg" }, body = "console.log(msg);")
1.38 + private static void log(String msg) {
1.39 + }
1.40
1.41 + @Override
1.42 public void run() {
1.43 - mainLoop(1);
1.44 + try {
1.45 + mainLoop(1);
1.46 + } finally {
1.47 + synchronized (queue) {
1.48 + if (!queue.isEmpty()) {
1.49 + long next = queue.getMin().nextExecutionTime;
1.50 + long now = System.currentTimeMillis();
1.51 + int delta = (int) (next - now);
1.52 + notifyQueue(delta);
1.53 + }
1.54 + }
1.55 + }
1.56 // try {
1.57 // mainLoop(0);
1.58 // } finally {
1.59 @@ -561,8 +589,12 @@
1.60 return;
1.61 }
1.62 }
1.63 - if (taskFired) // Task fired; run it, holding no locks
1.64 + if (taskFired) {
1.65 + // Task fired; run it, holding no locks
1.66 + log("Running " + task);
1.67 task.run();
1.68 + log("Done running " + task);
1.69 + }
1.70 } catch(Exception e) {
1.71 e.printStackTrace();
1.72 }
2.1 --- a/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/TimerTest.java Wed Apr 27 04:01:39 2016 +0200
2.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/TimerTest.java Wed Apr 27 05:49:41 2016 +0200
2.3 @@ -28,6 +28,7 @@
2.4 * @author Jaroslav Tulach <jtulach@netbeans.org>
2.5 */
2.6 public class TimerTest {
2.7 + boolean started;
2.8 int miss;
2.9 int exec;
2.10
2.11 @@ -35,15 +36,18 @@
2.12 }
2.13
2.14 @BrwsrTest public void scheduleTick() throws Exception {
2.15 - Timer t = new Timer("MyTest");
2.16 - class TT extends TimerTask {
2.17 - @Override
2.18 - public void run() {
2.19 - exec++;
2.20 + if (!started) {
2.21 + started = true;
2.22 + Timer t = new Timer("MyTest");
2.23 + class TT extends TimerTask {
2.24 + @Override
2.25 + public void run() {
2.26 + exec++;
2.27 + }
2.28 }
2.29 + TT task = new TT();
2.30 + t.schedule(task, 15);
2.31 }
2.32 - TT task = new TT();
2.33 - t.schedule(task, 15);
2.34
2.35 if (exec == 0) {
2.36 miss++;
2.37 @@ -55,15 +59,20 @@
2.38 }
2.39
2.40 @BrwsrTest public void repeatedTicks() throws Exception {
2.41 - Timer t = new Timer("MyTest");
2.42 - class TT extends TimerTask {
2.43 - @Override
2.44 - public void run() {
2.45 - exec++;
2.46 + if (!started) {
2.47 + started = true;
2.48 + Timer t = new Timer("MyTest");
2.49 + class TT extends TimerTask {
2.50 + @Override
2.51 + public void run() {
2.52 + if (++exec == 2) {
2.53 + cancel();
2.54 + }
2.55 + }
2.56 }
2.57 + TT task = new TT();
2.58 + t.scheduleAtFixedRate(task, 50, 70);
2.59 }
2.60 - TT task = new TT();
2.61 - t.scheduleAtFixedRate(task, 15, 10);
2.62
2.63 if (exec != 2) {
2.64 miss++;