简体   繁体   English

() - > System.out.println(“done”)是什么意思?

[英]What is the meaning of ()->System.out.println(“done”)?

In Concurrency Interest link, there is a code which is like this:- Concurrency Interest链接中,有一个代码如下: -

exec.schedule( ()-> System.out.println("done"),
         1, TimeUnit.SECONDS );

What is the meaning of ()-> ? () - >是什么意思?

I checked in eclipse, it does not allow. 我检查了eclipse,它不允许。 But what was the intention of the thread-writer? 但线程编写者的意图是什么?

This is Lambda syntax from JDK8. 这是来自JDK8的Lambda语法。

It is pretty similar (but not exactly same) to 它非常相似(但不完全相同)

exec.schedule(new Runnable() { 
    public void run() {
        System.out.println("done");
    }
}, 1, TimeUnit.SECONDS);

That is the Java 8 syntax for Lambda Expressions . 这是Lambda Expressions的Java 8语法。

The ScheduledThreadPoolExecutor#exec(..) method expects a Runnable argument. ScheduledThreadPoolExecutor#exec(..)方法需要一个Runnable参数。 Runnable is a functional interface because it only contains one abstract method. Runnable是一个功能接口,因为它只包含一个abstract方法。 As such, the compiler can infer that you are defining a new Runnable instance with the lambda. 因此,编译器可以推断您正在使用lambda定义新的Runnable实例。

The parts between () are the run() method's parameters, ie. ()之间的部分是run()方法的参数,即。 none. 没有。 The part after the -> is the body of the method. ->之后的部分是方法的主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM