最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java多種方法實(shí)現(xiàn)等待所有子線程完成再繼續(xù)執(zhí)行

2023-03-10 07:55 作者:程序員-王堅(jiān)  | 我要投稿

簡介

在現(xiàn)實(shí)世界中,我們常常需要等待其它任務(wù)完成,才能繼續(xù)執(zhí)行下一步。Java實(shí)現(xiàn)等待子線程完成再繼續(xù)執(zhí)行的方式很多。我們來一一查看一下。

Thread的join方法

該方法是Thread提供的方法,調(diào)用join()時(shí),會(huì)阻塞主線程,等該Thread完成才會(huì)繼續(xù)執(zhí)行,代碼如下:

private static void threadJoin() { ?List<Thread> threads = new ArrayList<>(); ?for (int i = 0; i < NUM; i++) { ? ?Thread t = new Thread(new PkslowTask("Task " + i)); ? ?t.start(); ? ?threads.add(t); ?} ?threads.forEach(t -> { ? ?try { ? ? ?t.join(); ? ?} catch (InterruptedException e) { ? ? ?throw new RuntimeException(e); ? ?} ?}); ?System.out.println("threadJoin Finished All Tasks..."); }

結(jié)果:

Task 6 is running Task 9 is running Task 3 is running Task 4 is running Task 7 is running Task 0 is running Task 2 is running Task 1 is running Task 5 is running Task 8 is running Task 1 is completed Task 8 is completed Task 6 is completed Task 4 is completed Task 3 is completed Task 0 is completed Task 7 is completed Task 9 is completed Task 2 is completed Task 5 is completed threadJoin Finished All Tasks...

CountDownLatch

CountDownLatch是一個(gè)很好用的并發(fā)工具,初始化時(shí)要指定線程數(shù),如10。在子線程調(diào)用countDown()時(shí)計(jì)數(shù)減1。直到為0時(shí),await()方法才不會(huì)阻塞。代碼如下:

private static void countDownLatch() { ?CountDownLatch latch = new CountDownLatch(NUM); ?for (int i = 0; i < NUM; i++) { ? ?Thread t = new Thread(() -> { ? ? ?System.out.println("countDownLatch running..."); ? ? ?try { ? ? ? ?Thread.sleep(1000); ? ? ? ?System.out.println("countDownLatch Finished..."); ? ? ? ?latch.countDown(); ? ? ?} catch (InterruptedException e) { ? ? ? ?throw new RuntimeException(e); ? ? ?} ? ?}); ? ?t.start(); ?} ?try { ? ?latch.await(); ?} catch (InterruptedException e) { ? ?throw new RuntimeException(e); ?} ?System.out.println("countDownLatch Finished All Tasks..."); }

結(jié)果:

countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch running... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished... countDownLatch Finished All Tasks...

CyclicBarrier

CyclicBarrier與CountDownLatch類似,但CyclicBarrier可重置,可重用。代碼如下:

private static void cyclicBarrier() { ?CyclicBarrier barrier = new CyclicBarrier(NUM + 1); ?for (int i = 0; i < NUM; i++) { ? ?Thread t = new Thread(() -> { ? ? ?System.out.println("cyclicBarrier running..."); ? ? ?try { ? ? ? ?Thread.sleep(1000); ? ? ? ?System.out.println("cyclicBarrier Finished..."); ? ? ? ?barrier.await(); ? ? ?} catch (InterruptedException | BrokenBarrierException e) { ? ? ? ?throw new RuntimeException(e); ? ? ?} ? ?}); ? ?t.start(); ?} ?try { ? ?barrier.await(); ?} catch (InterruptedException | BrokenBarrierException e) { ? ?throw new RuntimeException(e); ?} ?System.out.println("cyclicBarrier Finished All Tasks..."); }

結(jié)果:

cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier running... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished... cyclicBarrier Finished All Tasks...

executorService.isTerminated()

ExecutorService調(diào)用shutdown()方法后,可以通過方法isTerminated()來判斷任務(wù)是否完成。代碼如下:

private static void executeServiceIsTerminated() { ?ExecutorService executorService = Executors.newFixedThreadPool(THREADS); ?IntStream.range(0, NUM) ? ?.forEach(i -> executorService.execute(new PkslowTask("Task " + i))); ?executorService.shutdown(); ?while (!executorService.isTerminated()) { ? ?//waiting... ?} ?System.out.println("executeServiceIsTerminated Finished All Tasks..."); }

結(jié)果:

Task 0 is running Task 2 is running Task 1 is running Task 3 is running Task 4 is running Task 0 is completed Task 2 is completed Task 5 is running Task 4 is completed Task 7 is running Task 3 is completed Task 1 is completed Task 8 is running Task 6 is running Task 9 is running Task 5 is completed Task 9 is completed Task 7 is completed Task 6 is completed Task 8 is completed executeServiceIsTerminated Finished All Tasks...

executorService.awaitTermination

executorService.awaitTermination方法會(huì)等待任務(wù)完成,并給一個(gè)超時(shí)時(shí)間,代碼如下:

private static void executeServiceAwaitTermination() { ?ExecutorService executorService = Executors.newFixedThreadPool(THREADS); ?IntStream.range(0, NUM) ? ?.forEach(i -> executorService.execute(new PkslowTask("Task " + i))); ?executorService.shutdown(); ?try { ? ?if (!executorService.awaitTermination(1, TimeUnit.MINUTES)) { ? ? ?executorService.shutdownNow(); ? ?} ?} catch (InterruptedException e) { ? ?throw new RuntimeException(e); ?} ?System.out.println("executeServiceAwaitTermination Finished All Tasks..."); }

結(jié)果:

Task 0 is running Task 1 is running Task 2 is running Task 3 is running Task 4 is running Task 0 is completed Task 5 is running Task 1 is completed Task 4 is completed Task 7 is running Task 3 is completed Task 8 is running Task 2 is completed Task 9 is running Task 6 is running Task 5 is completed Task 7 is completed Task 9 is completed Task 8 is completed Task 6 is completed executeServiceAwaitTermination Finished All Tasks...

executorService.invokeAll

使用invokeAll提交所有任務(wù),代碼如下:

private static void executeServiceInvokeAll() { ?ExecutorService executorService = Executors.newFixedThreadPool(THREADS); ?List<Callable<Void>> tasks = new ArrayList<>(); ?IntStream.range(0, NUM) ? ?.forEach(i -> tasks.add(new PkslowTask("Task " + i))); ?try { ? ?executorService.invokeAll(tasks); ?} catch (InterruptedException e) { ? ?throw new RuntimeException(e); ?} ?executorService.shutdown(); ?System.out.println("executeServiceInvokeAll Finished All Tasks..."); }

結(jié)果:

Task 1 is running Task 2 is running Task 0 is running Task 3 is running Task 4 is running Task 1 is completed Task 3 is completed Task 0 is completed Task 2 is completed Task 4 is completed Task 8 is running Task 5 is running Task 6 is running Task 9 is running Task 7 is running Task 8 is completed Task 5 is completed Task 6 is completed Task 9 is completed Task 7 is completed executeServiceInvokeAll Finished All Tasks...

ExecutorCompletionService

ExecutorCompletionService通過take()方法,會(huì)返回最早完成的任務(wù),代碼如下:

private static void executorCompletionService() { ?ExecutorService executorService = Executors.newFixedThreadPool(10); ?CompletionService<String> service = new ExecutorCompletionService<>(executorService); ?List<Callable<String>> callables = new ArrayList<>(); ?callables.add(new DelayedCallable(2000, "2000ms")); ?callables.add(new DelayedCallable(1500, "1500ms")); ?callables.add(new DelayedCallable(6000, "6000ms")); ?callables.add(new DelayedCallable(2500, "2500ms")); ?callables.add(new DelayedCallable(300, "300ms")); ?callables.add(new DelayedCallable(3000, "3000ms")); ?callables.add(new DelayedCallable(1100, "1100ms")); ?callables.add(new DelayedCallable(100, "100ms")); ?callables.add(new DelayedCallable(100, "100ms")); ?callables.add(new DelayedCallable(100, "100ms")); ?callables.forEach(service::submit); ?for (int i = 0; i < NUM; i++) { ? ?try { ? ? ?Future<String> future = service.take(); ? ? ?System.out.println(future.get() + " task is completed"); ? ?} catch (InterruptedException | ExecutionException e) { ? ? ?throw new RuntimeException(e); ? ?} ?} ?System.out.println("executorCompletionService Finished All Tasks..."); ?executorService.shutdown(); ?awaitTerminationAfterShutdown(executorService); }

這里不同任務(wù)的時(shí)長是不一樣的,但會(huì)先返回最早完成的任務(wù):

2000ms is running 2500ms is running 300ms is running 1500ms is running 6000ms is running 3000ms is running 1100ms is running 100ms is running 100ms is running 100ms is running 100ms is completed 100ms is completed 100ms task is completed 100ms task is completed 100ms is completed 100ms task is completed 300ms is completed 300ms task is completed 1100ms is completed 1100ms task is completed 1500ms is completed 1500ms task is completed 2000ms is completed 2000ms task is completed 2500ms is completed 2500ms task is completed 3000ms is completed 3000ms task is completed 6000ms is completed 6000ms task is completed executorCompletionService Finished All Tasks...


Java多種方法實(shí)現(xiàn)等待所有子線程完成再繼續(xù)執(zhí)行的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
嘉黎县| 蒙城县| 墨玉县| 浦县| 定远县| 鹤山市| 蓬莱市| 宁城县| 册亨县| 平武县| 措美县| 高雄市| 同德县| 阳原县| 饶河县| 和硕县| 南开区| 饶平县| 阜康市| 榆树市| 开平市| 信丰县| 宾阳县| 高台县| 南平市| 东辽县| 美姑县| 清水河县| 宁德市| 亚东县| 准格尔旗| 建始县| 南宫市| 呼和浩特市| 丹江口市| 仁怀市| 潮安县| 西青区| 宜兰市| 历史| 景洪市|