How can a Java lambda-expression reference itself?

后端 未结 3 1894
悲哀的现实
悲哀的现实 2021-02-04 16:27

I found this article to be very informative in comparison of old-style functions to new Java-8 lambda-functions and parallel processing. One thing I couldn\'t quite understand w

3条回答
  •  失恋的感觉
    2021-02-04 17:06

    Derived from @Alex's answer:

    @FunctionalInterface
    public interface SelfRunnable extends Runnable {
      public void run(SelfRunnable this_);
    
      @Override
      public default void run() {
        run(this);
      }
    
      public static Runnable runnable(SelfRunnable runnable) {
        return runnable;
      }
    }
    
    public interface Test {
      public static void main(String... arguments) {
        final Runnable r = SelfRunnable.runnable(this_ -> this_.run());
        r.run();
      }
    }
    

提交回复
热议问题