面试官:如何让主线程等待所有的子线程结束之后再执行?我懵了

使用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("主线程结束"); 
  31.     } 

 join()方法使得主线程等待子线程执行结束,阻塞的是主线程。其底层原理,可以参考我的这篇文章你真得懂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.         //不再接受新的任务 
  32.         pool.shutdown(); 
  33.          
  34.         while (true) { 
  35.             //手动循环确实效率很低,不推荐 
  36.             if (pool.isTerminated()) { 
  37.                 System.out.println("线程池中的任务执行结束"); 
  38.                 break; 
  39.             } 
  40.         } 
  41.         System.out.println("主线程结束"); 
  42.     } 

isTerminated,当调用shutdown()方法后,并且所有提交的任务完成后才会返回为true

这里直接使用了固定大小的线程池,线程池的参数在面试中也经常被问到,对线程池不熟悉的同学,可以参考我的这篇文章说说线程池

使用Future机制

 
 
 
  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.         //不再接受新的任务 
  36.         pool.shutdown(); 
  37.          
  38.         //get方法为阻塞获取 
  39.         System.out.println("task1的运行结果:" + task1.get()); 
  40.         System.out.println("task2的运行结果:" + task2.get()); 
  41.  
  42.         System.out.println("主线程结束"); 
  43.     } 

Future机制,可以参考我的另外一篇博客谈谈Future、Callable、FutureTask关系

使用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("主线程结束"); 
  38.     } 

每调用一次countDown方法,计数器会减1,在计数器减为0之前,await方法将会阻塞主线程。有关CountDownLatch的底层原理,可以参考我的另外一篇博客CountDownLatch实现原理

使用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("计算结果为" + cf.get()); 
  33.         System.out.println("主线程结束"); 
  34.     } 

等到两个子任务都完成后,输出两数之积,再执行主线程。对CompletableFuture不熟悉的同学,可以参考我的这一篇文章什么,你还不会用CompletableFuture?

本文名称:面试官:如何让主线程等待所有的子线程结束之后再执行?我懵了
转载来于:http://www.shufengxianlan.com/qtweb/news24/424074.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联