Java多線程面試題:假如有Thread1、Thread2、Thread3、Thread4四條線程分別統計C、D、E、F四個盤的大小,所有線程都統計完畢交給Thread5線程去做匯總,應當如何實現?


利用java.util.concurrent包下的CountDownLatch(減數器)或CyclicBarrier(循環柵欄)可以實現此類問題


 

1. 利用CountDownLatch 的代碼實現

public class test1{
public static void main(String[] args) throws Exception {
        //創建一個能容納4個線程的減數器
        final CountDownLatch countDownLatch= new CountDownLatch(4);
        Runnable runC= new Runnable() {
            
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("統計C盤");
                    countDownLatch.countDown();//單任務,把計數器減1
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Runnable runD= new Runnable() {
            
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("統計D盤");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Runnable runE= new Runnable() {
            
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("統計E盤");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Runnable runF= new Runnable() {
            
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("統計F盤");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        
        ExecutorService service= Executors.newFixedThreadPool(4);
        service.submit(runC);
        service.submit(runD);
        service.submit(runE);
        service.submit(runF);
        
        countDownLatch.await();//主線程,即第5線程等待
        System.out.println("合計C,D,E,F");
        service.shutdown();
}

}

  

2. 利用CyclicBarrier的代碼實現

public class test2{
public static void main(String[] args) {
        Runnable barrierAction= new Runnable() {
            @Override
            public void run() {
                System.out.println("統計C,D,E,F盤");
            }
        };
        
        final CyclicBarrier cyclicBarrier= new CyclicBarrier(4, barrierAction );
 
       Runnable runC= new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("C盤");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                
            }
        };

        Runnable runD= new Runnable() {   
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("D盤");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                
            }
        };
 
       Runnable runE= new Runnable() {           
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("E盤");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                
            }
        };
 
       Runnable runF= new Runnable() {            
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("F盤");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                
            }
        };
        
        ExecutorService service= Executors .newFixedThreadPool(4);
        service.submit(runC);
        service.submit(runD);
        service.submit(runE);
        service.submit(runF);
        service.shutdown();
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM