热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

理解java并发工具Phaser

java为我们提供了很多并发工具,比如Semaphore、CountDownLatch、CyclicBarrier还有我们这里要讲到的phaser。当某些任务是分成

java为我们提供了很多并发工具,比如Semaphore、CountDownLatch、CyclicBarrier还有我们这里要讲到的phaser。

当某些任务是分成多个步骤来执行时,并且同一个步骤之间可以并发,不同步骤之间不能并发的时候, 此机制是非常有用的。Phaser类提供的机制是在每个步骤的结尾同步线程,所以除非全部线程完成第一个步骤,否则线程不能开始进行第二步。

使用phaser你首先需要指示参与同步的线程的个数,我们可以使用 phaser.register()或者new Phaser(5),下面的示意图给出了形象的解释:

我们假设每个方框代表需要同步的线程,这里直接使用new Phaser(4)说明有4个线程需要同步:
在这里插入图片描述
如果在同一个phaser对象上使用phaser.register(),每使用一次就增加一个需要同步的线程。也就是增加一个方框的数量。

如果现在有线程调用了:phaser.arriveAndAwaitAdvance()那就说明有一个线程到达了同步点,它将阻塞在调用这个函数的位置,直到所有需要同步的线程都到达,将对所有线程统一放行,进行第二个阶段任务的执行。
在这里插入图片描述
在第二阶段的时候,又会有这四个方框被创建出来,等待线程的到达,然后又统一放行,最后直到方框数量为0,方框数量是如何降到0的,就需要了解如果减少方框的数量了。

我们在执行期间,可以去增加或者减少这些方框的数量。增加的话依旧依旧可以使用phaser.register()如果需要减少方框的数量可以调用phaser.arriveAndDeregister(),也就是通知Phaser对象,当前线程已经结束这个阶段,并且将不再参与接下来的阶段操作。这就相当于说明这个阶段到达了方框,并且下一个阶段减少一个方框。一般一个线程的所有阶段任务都结束了,需要调用phaser.arriveAndDeregister();或者一个线程在执行的时候我们希望停止这个线程的执行了,那么可以调用phaser.arriveAndDeregister();通知phaser后面阶段不用同步这个线程了。

Phaser还提供了一个方法protected boolean onAdvance(int phase, int registeredParties),这个方法每当统一放行的时候会自动执行一次,它接收2个参数:当前阶段数(从0开始)和注册的参与者数;

在这里有一个例子,它实现一个模拟测验,所有学生要完成他们的练习。全部的学生都必须完成同一个练习才能继续下一个练习。作为一个练习,你可以将例子做个扩展,比如如果解题时间(代码中表现为线程睡眠时间)大于8s,就说明学生做不出来这一题,则退出测验。

下面是例子的输出,一个线程代表一个学生:

Thread-4: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019
Thread-4: Has done the first exercise. Mon Jul 22 13:40:50 CST 2019
Thread-3: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019
Thread-0: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019
Thread-2: Has done the first exercise. Mon Jul 22 13:40:52 CST 2019
Thread-0: Has done the first exercise. Mon Jul 22 13:40:55 CST 2019
Thread-1: Has done the first exercise. Mon Jul 22 13:40:56 CST 2019
Thread-3: Has done the first exercise. Mon Jul 22 13:40:57 CST 2019
Phaser: All the students has finished the first exercise.
Phaser: It's turn for the second one.
Thread-1: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-0: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-2: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-2: Has done the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-4: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-3: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019
Thread-0: Has done the second exercise. Mon Jul 22 13:40:59 CST 2019
Thread-4: Has done the second exercise. Mon Jul 22 13:40:59 CST 2019
Thread-3: Has done the second exercise. Mon Jul 22 13:41:03 CST 2019
Thread-1: Has done the second exercise. Mon Jul 22 13:41:04 CST 2019
Phaser: All the students has finished the second exercise.
Phaser: It's turn for the third one.
Thread-3: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019
Thread-3: Has finished the exam. Mon Jul 22 13:41:04 CST 2019
Thread-4: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019
Thread-0: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019
Thread-2: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019
Thread-1: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019
Thread-0: Has finished the exam. Mon Jul 22 13:41:06 CST 2019
Thread-4: Has finished the exam. Mon Jul 22 13:41:09 CST 2019
Thread-2: Has finished the exam. Mon Jul 22 13:41:09 CST 2019
Thread-1: Has finished the exam. Mon Jul 22 13:41:09 CST 2019
Phaser: All the students has finished the exam.
Phaser: Thank you for your time.
Main: The phaser has finished: true.

下面是添加了模拟学生不会做的情况的输出:

Thread-1: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019
Thread-4: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019
Thread-3: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019
Thread-0: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019
Thread-2: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019
Phaser: The exam are going to start. The students are ready.
Phaser: We have 5 students.
Thread-2: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-2: Exit the exam
Thread-4: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-4: Exit the exam
Thread-0: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-0: Has done the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-3: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-1: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019
Thread-1: Exit the exam
Thread-3: Has done the first exercise. Mon Jul 22 14:00:14 CST 2019
Phaser: All the students has finished the first exercise.
Phaser: It's turn for the second one.
Thread-0: Is going to do the second exercise. Mon Jul 22 14:00:14 CST 2019
Thread-0: Exit the exam
Thread-3: Is going to do the second exercise. Mon Jul 22 14:00:14 CST 2019
Thread-3: Has done the second exercise. Mon Jul 22 14:00:19 CST 2019
Phaser: All the students has finished the second exercise.
Phaser: It's turn for the third one.
Thread-3: Is going to do the third exercise. Mon Jul 22 14:00:19 CST 2019
Thread-3: Exit the exam
Phaser: All the students has finished the exam.
Phaser: Thank you for your time.
Main: The phaser has finished: true.

推荐阅读
  • JUC并发编程——线程的基本方法使用
    目录一、线程名称设置和获取二、线程的sleep()三、线程的interrupt四、join()五、yield()六、wait(),notify(),notifyAll( ... [详细]
  • 问题描述现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能;在实际开发过程中 ... [详细]
  • pypy 真的能让 Python 比 C 还快么?
    作者:肖恩顿来源:游戏不存在最近“pypy为什么能让python比c还快”刷屏了,原文讲的内容偏理论,干货比较少。我们可以再深入一点点,了解pypy的真相。正式开始之前,多唠叨两句 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 长期从事ABAP开发工作的专业人士,在面对行业新趋势时,往往需要重新审视自己的发展方向。本文探讨了几位资深专家对ABAP未来走向的看法,以及开发者应如何调整技能以适应新的技术环境。 ... [详细]
  • Irish budget airline Ryanair announced plans to significantly increase its route network from Frankfurt Airport, marking a direct challenge to Lufthansa, Germany's leading carrier. ... [详细]
  • 本文深入探讨了Go语言中的接口型函数,通过实例分析其灵活性和强大功能,帮助开发者更好地理解和运用这一特性。 ... [详细]
  • 本文将详细介绍如何使用Java编程语言生成指定数量的不重复随机数,包括具体的实现方法和代码示例。适合初学者和有一定基础的开发者参考。 ... [详细]
  • spring boot使用jetty无法启动 ... [详细]
  • importjava.io.*;importjava.util.*;publicclass五子棋游戏{staticintm1;staticintn1;staticfinalintS ... [详细]
  • 实现系统调用
    实现系统调用一、实验环境​本次操作还是基于上次编译Linux0.11内核的实验环境进行操作。环境如下:二、实验目标​通过对上述实验原理的认识,相信 ... [详细]
  • Flutter 核心技术与混合开发模式深入解析
    本文深入探讨了 Flutter 的核心技术,特别是其混合开发模式,包括统一管理模式和三端分离模式,以及混合栈原理。通过对比不同模式的优缺点,帮助开发者选择最适合项目的混合开发策略。 ... [详细]
  • 协程作为一种并发设计模式,能有效简化Android平台上的异步代码处理。自Kotlin 1.3版本引入协程以来,这一特性基于其他语言的成熟理念,为开发者提供了新的工具,以增强应用的响应性和效率。 ... [详细]
  • 本文探讨了一种统一的语义数据模型,旨在支持物联网、建筑及企业环境下的数据转换。该模型强调简洁性和可扩展性,以促进不同行业间的插件化和互操作性。对于智能硬件开发者而言,这一模型提供了重要的参考价值。 ... [详细]
  • RTThread线程间通信
    线程中通信在裸机编程中,经常会使用全局变量进行功能间的通信,如某些功能可能由于一些操作而改变全局变量的值,另一个功能对此全局变量进行读取& ... [详细]
author-avatar
祖巧爽_940
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有