Java 8:函数式接口 Runnable:无参数, 无返回值


#Java 8#


本文关键词:Java 8, 函数式接口,lambda 。

介绍

全路径: java.lang.Runnable

该接口在 Java 8 之前就存在,从 Java 8 开始加了注解 @FunctionalInterface。一般用于多线程开发中。

源码:

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

示例

代码示例:

public class Main {

    public static void main(String[] args) {
        Runnable runnable = () -> {
            System.out.println("你好");
        };
        runnable.run();  // 输出: 你好
    }

}

( 本文完 )