探討主線程等待子線程執行完畢的方法

實現子線程未執行完主線程阻塞的方式有很多種,不過大致原理是計數,確保都執行完畢或者通過主線程和所有子線程保證順序執行的方式,對於countDownLatch以及java線程池的都是concurent下面的jar包

方法1:用sleep方法,讓主線程睡眠一段時間,當然這個睡眠時間是主觀的時間,是我們自己定的,這個方法不推薦

 new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<1000;i++){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(2222);

 

方法2:使用Thread的join()等待所有的子線程執行完畢,主線程在執行,thread.join()把指定的線程加入到當前線程,可以將兩個交替執行的線程合併爲順序執行的線程。比如在線程B中調用了線程A的Join()方法,直到線程A執行完畢後,纔會繼續執行線程B

Vector<Thread> threadVector=new Vector<>();
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2222);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        threadVector.add(thread1);
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3333);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();
        threadVector.add(thread2);
        for (Thread thread : threadVector) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(3333);


方法3:countDownLatch計數法

 final CountDownLatch countDownLatch=new CountDownLatch(2);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1111);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2222);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("子線程countDownLatch執行完畢");

方法4:java線程池的執行方式

  ExecutorService executorService= Executors.newFixedThreadPool(2);
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1111);
                    System.out.println("java線程池1");
                    executorService.shutdown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1111);
                    System.out.println("java線程池2");
                    executorService.shutdown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        while (true){
            if(executorService.isTerminated()){
                System.out.println("所有子線程都結束了");
                break;

            }
        }

 

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