How to use TimerTask with lambdas?

后端 未结 5 1702
逝去的感伤
逝去的感伤 2020-12-05 14:29

As you hopefully know you can use lambdas in Java 8, for example to replace anonymous methods.

An example can be seen here of Java 7 vs Java 8:

Runna         


        
5条回答
  •  借酒劲吻你
    2020-12-05 14:58

    I know this is an old post, but for completeness, I wanted to include the wrapper solution posted by Flown:

    private static TimerTask wrap(Runnable r) {
      return new TimerTask() {
    
        @Override
        public void run() {
          r.run();
        }
      };
    }
    

    then your call can become:

    timer.schedule(wrap(this::checkDirectory), delay);
    

提交回复
热议问题