rt/emul/compact/src/main/java/java/util/Timer.java
changeset 1945 a139403de819
parent 1407 32e050a07754
     1.1 --- a/rt/emul/compact/src/main/java/java/util/Timer.java	Sat Nov 02 21:09:52 2013 +0100
     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              }