lambda表達式創建多線程

1.普通開啓多線程

    public static void main(String[] args) {
        new Thread(() -> System.out.println("xzh")).start();
    }

 

2.線程池開啓多線程

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        Future<String> xzh = executor.submit(() -> test("xzh"));
        System.out.println(xzh.get());
        //關閉啓動線程
        executor.shutdown();
        //子線程是否全部結束
        boolean terminated = executor.isTerminated();
    }

    public static String test(String s){
        System.out.println(s);
        return "success";
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章