当前位置: 首页 > news >正文

商丘做网站张北京网站建设熊掌号

商丘做网站张,北京网站建设熊掌号,陕西多地最新通知,秋风最新消息简介 在现实世界中#xff0c;我们常常需要等待其它任务完成#xff0c;才能继续执行下一步。Java实现等待子线程完成再继续执行的方式很多。我们来一一查看一下。 Thread的join方法 该方法是Thread提供的方法#xff0c;调用join()时#xff0c;会阻塞主线程#xff0…简介 在现实世界中我们常常需要等待其它任务完成才能继续执行下一步。Java实现等待子线程完成再继续执行的方式很多。我们来一一查看一下。 Thread的join方法 该方法是Thread提供的方法调用join()时会阻塞主线程等该Thread完成才会继续执行代码如下 private static void threadJoin() {ListThread 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...);} 结果 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是一个很好用的并发工具初始化时要指定线程数如10。在子线程调用countDown()时计数减1。直到为0时await()方法才不会阻塞。代码如下 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...); } 结果 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...); } 结果 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调用shutdown()方法后可以通过方法isTerminated()来判断任务是否完成。代码如下 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...);} 结果 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方法会等待任务完成并给一个超时时间代码如下 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...); } 结果 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提交所有任务代码如下 private static void executeServiceInvokeAll() {ExecutorService executorService Executors.newFixedThreadPool(THREADS);ListCallableVoid 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...); } 结果 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()方法会返回最早完成的任务代码如下 private static void executorCompletionService() {ExecutorService executorService Executors.newFixedThreadPool(10);CompletionServiceString service new ExecutorCompletionService(executorService);ListCallableString 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 {FutureString 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); } 这里不同任务的时长是不一样的但会先返回最早完成的任务 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... 代码 代码请看GitHub: https://github.com/LarryDpk/pkslow-samples
http://www.w-s-a.com/news/718579/

相关文章:

  • 郑州那家做网站便宜商业计划书免费word版
  • 秦时明月的个人网站怎么做网站开发公司需要招聘哪些人
  • 广告网站建设制作设计服务商安卓app软件定制
  • 公司网站设计与实现中国职业培训在线官方网站
  • 网站服务器空间租用郑州官网网站推广优化
  • 郑州网站建设外包业务wordpress站酷首页
  • 机关门户网站 建设 方案个人怎么申请注册商标
  • 梧州网站建设有哪些九江网站建设优化
  • APP网站建设开发企业发展英文seo招聘
  • 临海市住房和城乡建设规划局网站高校图书馆网站的建设方案
  • 建立门户网站张店易宝网站建设
  • wordpress中英文站点厦门seo顾问屈兴东
  • 邯郸网站建设项目重庆网站备案系统
  • 网站导航容易做黄冈网站建设报价
  • 美橙互联建站网站被截止徐州网站建站
  • 网站班级文化建设视频深圳企业网页设计公司
  • 钦州网站建设公司做宣传网站买什么云服务器
  • 58同城有做网站wordpress怎么改标题和meta
  • 安通建设有限公司网站东莞地铁app
  • 群晖nas做网站滨州教育平台 网站建设
  • 住房城市乡建设部网站装修平台有哪些
  • 小米网站 用什么做的深圳广告公司前十强
  • 勤哲网站开发视频瑞安 网站建设培训
  • 有个蓝色章鱼做标志的网站高端的网站建设怎么做
  • 建站网址导航hao123html网页设计实验总结
  • 西宁市网站建设价格丽水集团网站建设
  • 长宁怎么做网站优化好本机怎么放自己做的网站
  • 诚信网站备案中心网站字体怎么设置
  • 企业网站建设费是无形资产吗佛山网站建设哪个好点
  • 网站建设就业方向国开行网站毕业申请怎么做