Lambda表达式-03-02

2019-03-02  本文已影响0人  封_绝

外部类实现线程接口:

public class TestLambda {
    public static void main(String[] args) {
        new Thread(new Race()).start();
    }

}
class Race implements Runnable{
    @Override
    public void run() {
            System.out.println(Thread.currentThread().getName() + "运行中");
    }
}

内部类实现线程接口:

public class TestLambda {
    public static void main(String[] args) {
        new Thread(new Race()).start();
    }

    //静态方法只能使用静态内部类
    static class Race implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "运行中");
        }
    }
}

匿名内部类:

public class TestLambda {
    public static void main(String[] args) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "运行中");
            }
        }).start();
    }
}

jdk8的Lambda表达式:

    public static void main(String[] args) {

        new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "运行中");
            }
        ).start();

    }
上一篇 下一篇

猜你喜欢

热点阅读