线程的执行顺序: join(),get(),isTerminated()

一般线程 join(),使下面的线程挂起听说:

/**
 * 如果没有join(),那样的话就是执行最后一句话,再执行线程二,再执行线程一
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程一");
        });
        thread1.start();

        // 记得,join()一定要在start()后面
        thread1.join();

        // 因为join,下面的线程呀等到thread1 执行完之后才能执行
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程二");
        }).start();

        System.out.println("执行完线程一再执行这个主线程再执行线程二");

    }
}
View Code

 线程池

方法一:

public class Main {

    /**
     * 线程池里面封装的几个工厂方法:要知道哦哦哦
     */
    private static ExecutorService executorService = Executors.newFixedThreadPool(2);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Future<?> submit1 = executorService.submit(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("####################线程1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Future<?> submit2 = executorService.submit(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("####################线程2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        /**
         * 不能用shutdownNow()
         * 因为shutdown()是关闭的时候,正在执行的线程等你全部执行完之后再正式关闭
         * shutdownNow()是立刻关闭所有全部线程,无论你现在的线程正在运行与否
         * 记得:如果发生异常,isTerminated()还是会返回true的,所以要get
         */
        executorService.shutdown();

        try {
            submit1.get();
            submit2.get();
        } catch (InterruptedException | ExecutionException e) {
            // 如果发生异常,执行这个
            System.out.println("$$$$$$$$$$$$$$$$$$$$$$");
            e.printStackTrace();
            throw e;
        }

        /**
         * isTerminated()方法中源码说:只有在shutdown()或者shutdownNow()方法之后,且线程全部执行完,才返回true
         */
//        while (true){
//            if (executorService.isTerminated()) {
//                System.out.println("线程执行完之后执行这个");
//                break;
//            }
//            TimeUnit.SECONDS.sleep(2);
//        }

        /**
         * 这个方法,类似于上面那个,只是多了一个这个功能:如果在下面的时间内还没有返回true,也继续往下执行
         */
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        System.out.println("线程执行完之后执行这个");

    }
}
View Code

 方法二:好奇怪啊,这个都可以

executorService.shutdown();

submit1.get();
submit2.get();

System.out.println("线程执行完之后执行这个");
View Code

方法三:用 CountDownLatch

https://www.cnblogs.com/ericguoxiaofeng/p/12322153.html

posted @ 2019-11-25 20:52  天马行空郭  阅读(442)  评论(0编辑  收藏  举报