日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
面試官:如何讓主線程等待所有的子線程結(jié)束之后再執(zhí)行?我懵了

使用Thread的join方法

 
 
 
  1. package com.qcy.testThreadFinish; 
  2.  
  3. /** 
  4.  * @author qcy 
  5.  * @create 2020/09/09 17:05:23 
  6.  */ 
  7. public class Case1 { 
  8.     public static void main(String[] args) throws InterruptedException { 
  9.  
  10.         Thread t1 = new Thread(() -> { 
  11.             try { 
  12.                 Thread.sleep(3000); 
  13.             } catch (InterruptedException e) { 
  14.                 e.printStackTrace(); 
  15.             } 
  16.         }); 
  17.         t1.start(); 
  18.  
  19.         Thread t2 = new Thread(() -> { 
  20.             try { 
  21.                 Thread.sleep(3000); 
  22.             } catch (InterruptedException e) { 
  23.                 e.printStackTrace(); 
  24.             } 
  25.         }); 
  26.         t2.start(); 
  27.  
  28.         t1.join(); 
  29.         t2.join(); 
  30.         System.out.println("主線程結(jié)束"); 
  31.     } 

 join()方法使得主線程等待子線程執(zhí)行結(jié)束,阻塞的是主線程。其底層原理,可以參考我的這篇文章你真得懂Thread.join嗎?

使用線程池的isTerminated方法

 
 
 
  1. package com.qcy.testThreadFinish; 
  2.  
  3. import java.util.concurrent.ExecutorService; 
  4. import java.util.concurrent.Executors; 
  5.  
  6. /** 
  7.  * @author qcy 
  8.  * @create 2020/09/09 17:05:23 
  9.  */ 
  10. public class Case2 { 
  11.     public static void main(String[] args) { 
  12.  
  13.         ExecutorService pool = Executors.newFixedThreadPool(3); 
  14.  
  15.         pool.execute(() -> { 
  16.             try { 
  17.                 Thread.sleep(2000); 
  18.             } catch (InterruptedException e) { 
  19.                 e.printStackTrace(); 
  20.             } 
  21.         }); 
  22.  
  23.         pool.execute(() -> { 
  24.             try { 
  25.                 Thread.sleep(2000); 
  26.             } catch (InterruptedException e) { 
  27.                 e.printStackTrace(); 
  28.             } 
  29.         }); 
  30.  
  31.         //不再接受新的任務(wù) 
  32.         pool.shutdown(); 
  33.          
  34.         while (true) { 
  35.             //手動(dòng)循環(huán)確實(shí)效率很低,不推薦 
  36.             if (pool.isTerminated()) { 
  37.                 System.out.println("線程池中的任務(wù)執(zhí)行結(jié)束"); 
  38.                 break; 
  39.             } 
  40.         } 
  41.         System.out.println("主線程結(jié)束"); 
  42.     } 

isTerminated,當(dāng)調(diào)用shutdown()方法后,并且所有提交的任務(wù)完成后才會(huì)返回為true

這里直接使用了固定大小的線程池,線程池的參數(shù)在面試中也經(jīng)常被問(wèn)到,對(duì)線程池不熟悉的同學(xué),可以參考我的這篇文章說(shuō)說(shuō)線程池

使用Future機(jī)制

 
 
 
  1. package com.qcy.testThreadFinish; 
  2.  
  3. import java.util.concurrent.ExecutionException; 
  4. import java.util.concurrent.ExecutorService; 
  5. import java.util.concurrent.Executors; 
  6. import java.util.concurrent.Future; 
  7.  
  8. /** 
  9.  * @author qcy 
  10.  * @create 2020/09/09 17:05:23 
  11.  */ 
  12. public class Case4 { 
  13.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  14.  
  15.         ExecutorService pool = Executors.newFixedThreadPool(3); 
  16.  
  17.         Future task1 = pool.submit(() -> { 
  18.             try { 
  19.                 Thread.sleep(2000); 
  20.             } catch (InterruptedException e) { 
  21.                 e.printStackTrace(); 
  22.             } 
  23.             return 2; 
  24.         }); 
  25.  
  26.         Future task2 = pool.submit(() -> { 
  27.             try { 
  28.                 Thread.sleep(2000); 
  29.             } catch (InterruptedException e) { 
  30.                 e.printStackTrace(); 
  31.             } 
  32.             return 3; 
  33.         }); 
  34.  
  35.         //不再接受新的任務(wù) 
  36.         pool.shutdown(); 
  37.          
  38.         //get方法為阻塞獲取 
  39.         System.out.println("task1的運(yùn)行結(jié)果:" + task1.get()); 
  40.         System.out.println("task2的運(yùn)行結(jié)果:" + task2.get()); 
  41.  
  42.         System.out.println("主線程結(jié)束"); 
  43.     } 

Future機(jī)制,可以參考我的另外一篇博客談?wù)凢uture、Callable、FutureTask關(guān)系

使用CountDownLatch

 
 
 
  1. package com.qcy.testThreadFinish; 
  2.  
  3. import java.util.concurrent.CountDownLatch; 
  4.  
  5. /** 
  6.  * @author qcy 
  7.  * @create 2020/09/09 17:05:23 
  8.  */ 
  9. public class Case5 { 
  10.     public static void main(String[] args) throws InterruptedException { 
  11.  
  12.         CountDownLatch latch = new CountDownLatch(2); 
  13.  
  14.         Thread t1 = new Thread(() -> { 
  15.             try { 
  16.                 Thread.sleep(3000); 
  17.             } catch (InterruptedException e) { 
  18.                 e.printStackTrace(); 
  19.             } finally { 
  20.                 latch.countDown(); 
  21.             } 
  22.         }); 
  23.         t1.start(); 
  24.  
  25.         Thread t2 = new Thread(() -> { 
  26.             try { 
  27.                 Thread.sleep(3000); 
  28.             } catch (InterruptedException e) { 
  29.                 e.printStackTrace(); 
  30.             } finally { 
  31.                 latch.countDown(); 
  32.             } 
  33.         }); 
  34.         t2.start(); 
  35.  
  36.         latch.await(); 
  37.         System.out.println("主線程結(jié)束"); 
  38.     } 

每調(diào)用一次countDown方法,計(jì)數(shù)器會(huì)減1,在計(jì)數(shù)器減為0之前,await方法將會(huì)阻塞主線程。有關(guān)CountDownLatch的底層原理,可以參考我的另外一篇博客CountDownLatch實(shí)現(xiàn)原理

使用CompletableFuture

 
 
 
  1. package com.qcy.testThreadFinish; 
  2.  
  3. import java.util.concurrent.CompletableFuture; 
  4. import java.util.concurrent.ExecutionException; 
  5.  
  6. /** 
  7.  * @author qcy 
  8.  * @create 2020/09/09 17:05:23 
  9.  */ 
  10. public class Case6 { 
  11.     public static void main(String[] args) throws InterruptedException, ExecutionException { 
  12.  
  13.         CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> { 
  14.             try { 
  15.                 Thread.sleep(3000); 
  16.             } catch (InterruptedException e) { 
  17.                 e.printStackTrace(); 
  18.             } 
  19.             return 2; 
  20.         }); 
  21.  
  22.         CompletableFuture cf = CompletableFuture.supplyAsync(() -> { 
  23.             try { 
  24.                 Thread.sleep(3000); 
  25.             } catch (InterruptedException e) { 
  26.                 e.printStackTrace(); 
  27.             } 
  28.             return 3; 
  29.         }).thenCombine(cf1, (result1, result2) -> result1 * result2); 
  30.  
  31.         //get方法為阻塞獲取 
  32.         System.out.println("計(jì)算結(jié)果為" + cf.get()); 
  33.         System.out.println("主線程結(jié)束"); 
  34.     } 

等到兩個(gè)子任務(wù)都完成后,輸出兩數(shù)之積,再執(zhí)行主線程。對(duì)CompletableFuture不熟悉的同學(xué),可以參考我的這一篇文章什么,你還不會(huì)用CompletableFuture?


當(dāng)前名稱:面試官:如何讓主線程等待所有的子線程結(jié)束之后再執(zhí)行?我懵了
網(wǎng)站鏈接:http://www.dlmjj.cn/article/dpggecd.html