Using lamdas to schedule tasks
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 04:01:39 +0200
changeset 1944644d4f4bc6e0
parent 1943 4b4f9dbc807b
child 1945 a139403de819
Using lamdas to schedule tasks
ko/kosample/client/pom.xml
ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java
     1.1 --- a/ko/kosample/client/pom.xml	Tue Apr 26 08:35:46 2016 +0200
     1.2 +++ b/ko/kosample/client/pom.xml	Wed Apr 27 04:01:39 2016 +0200
     1.3 @@ -41,8 +41,8 @@
     1.4                <artifactId>maven-compiler-plugin</artifactId>
     1.5                <version>2.3.2</version>
     1.6                <configuration>
     1.7 -                  <source>1.7</source>
     1.8 -                  <target>1.7</target>
     1.9 +                  <source>1.8</source>
    1.10 +                  <target>1.8</target>
    1.11                </configuration>
    1.12            </plugin>
    1.13            <plugin>
     2.1 --- a/ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java	Tue Apr 26 08:35:46 2016 +0200
     2.2 +++ b/ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java	Wed Apr 27 04:01:39 2016 +0200
     2.3 @@ -17,6 +17,7 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.kosample;
     2.6  
     2.7 +import java.util.TimerTask;
     2.8  import net.java.html.json.ComputedProperty;
     2.9  import net.java.html.json.Function;
    2.10  import net.java.html.json.Model;
    2.11 @@ -53,15 +54,19 @@
    2.12          });
    2.13      }
    2.14  
    2.15 +    private static java.util.Timer TIMER = new java.util.Timer("Pending tasks");
    2.16 +    private static void schedule(Runnable run, long delay) {
    2.17 +        TIMER.schedule(new TimerTask() {
    2.18 +            @Override
    2.19 +            public void run() {
    2.20 +                run.run();
    2.21 +            }
    2.22 +        }, delay);
    2.23 +    }
    2.24 +
    2.25      @Function static void rotate5s(final Data model) {
    2.26          model.setRotating(true);
    2.27 -        java.util.Timer timer = new java.util.Timer("Rotates a while");
    2.28 -        timer.schedule(new java.util.TimerTask() {
    2.29 -            @Override
    2.30 -            public void run() {
    2.31 -                model.setRotating(false);
    2.32 -            }
    2.33 -        }, 5000);
    2.34 +        schedule(() -> model.setRotating(false), 5000);
    2.35      }
    2.36  
    2.37      @Function static void showScreenSize(Data model) {