本篇内容主要讲解“如何让主线程等待所有的子线程结束之后再执行”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何让主线程等待所有的子线程结束之后再执行”吧!
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); Future<Integer> task1 = pool.submit(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return 2; }); Future<Integer> 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 { CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return 2; }); CompletableFuture<Integer> 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("主线程结束"); } }
等到两个子任务都完成后,输出两数之积,再执行主线程。
到此,相信大家对“如何让主线程等待所有的子线程结束之后再执行”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。