理解java并发工具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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值