美文网首页
Lambda表达式-03-02

Lambda表达式-03-02

作者: 封_绝 | 来源:发表于2019-03-02 11:36 被阅读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();
    
        }
    

    相关文章

      网友评论

          本文标题:Lambda表达式-03-02

          本文链接:https://www.haomeiwen.com/subject/ndceuqtx.html