重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“如何让主线程等待所有的子线程结束之后再执行”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何让主线程等待所有的子线程结束之后再执行”吧!
10年积累的网站设计制作、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有潼关免费网站建设让你可以放心的选择与我们合作。
package com.qcy.testThreadFinish; /** * @author qcy * @create 2020/09/09 17:05:23 */ public class Case1 { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); Thread t2 = new Thread(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } }); t2.start(); t1.join(); t2.join(); System.out.println("主线程结束"); } }
join()方法使得主线程等待子线程执行结束,阻塞的是主线程。其底层原理,可以参考我的这篇文章你真得懂Thread.join吗?
package com.qcy.testThreadFinish; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @author qcy * @create 2020/09/09 17:05:23 */ public class Case2 { public static void main(String[] args) { ExecutorService pool = Executors.newFixedThreadPool(3); pool.execute(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }); pool.execute(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }); //不再接受新的任务 pool.shutdown(); while (true) { //手动循环确实效率很低,不推荐 if (pool.isTerminated()) { System.out.println("线程池中的任务执行结束"); break; } } System.out.println("主线程结束"); } }
isTerminated,当调用shutdown()方法后,并且所有提交的任务完成后才会返回为true
这里直接使用了固定大小的线程池,线程池的参数在面试中也经常被问到,对线程池不熟悉的同学,可以参考我的这篇文章说说线程池
package com.qcy.testThreadFinish; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * @author qcy * @create 2020/09/09 17:05:23 */ public class Case4 { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService pool = Executors.newFixedThreadPool(3); Futuretask1 = pool.submit(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return 2; }); Future task2 = pool.submit(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return 3; }); //不再接受新的任务 pool.shutdown(); //get方法为阻塞获取 System.out.println("task1的运行结果:" + task1.get()); System.out.println("task2的运行结果:" + task2.get()); System.out.println("主线程结束"); } }
Future机制,可以参考我的另外一篇博客谈谈Future、Callable、FutureTask关系
package com.qcy.testThreadFinish; import java.util.concurrent.CountDownLatch; /** * @author qcy * @create 2020/09/09 17:05:23 */ public class Case5 { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); Thread t1 = new Thread(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } finally { latch.countDown(); } }); t1.start(); Thread t2 = new Thread(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } finally { latch.countDown(); } }); t2.start(); latch.await(); System.out.println("主线程结束"); } }
每调用一次countDown方法,计数器会减1,在计数器减为0之前,await方法将会阻塞主线程。有关CountDownLatch的底层原理,可以参考我的另外一篇博客CountDownLatch实现原理
package com.qcy.testThreadFinish; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; /** * @author qcy * @create 2020/09/09 17:05:23 */ public class Case6 { public static void main(String[] args) throws InterruptedException, ExecutionException { CompletableFuturecf1 = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return 2; }); CompletableFuture cf = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return 3; }).thenCombine(cf1, (result1, result2) -> result1 * result2); //get方法为阻塞获取 System.out.println("计算结果为" + cf.get()); System.out.println("主线程结束"); } }
等到两个子任务都完成后,输出两数之积,再执行主线程。
到此,相信大家对“如何让主线程等待所有的子线程结束之后再执行”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!