Java:jsr166y Phaser的教程/解释 [英] Java: tutorials/explanations of jsr166y Phaser

查看:155
本文介绍了Java:jsr166y Phaser的教程/解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两年前,这个问题被问过,但资源



必须有一些好的教程才能理解 Phaser 。我读过javadoc,但是我的眼睛瞪了过来,因为为了真正了解javadoc,你必须知道这些类应该如何使用。



任何人有任何建议?

解决方案

对于Phaser我已经回答了几个问题。看到它们可能有助于理解他们的应用程序。它们在底部链接。但是要了解Phaser是什么,以及为什么它有用它重要的知道它解决什么。



以下是CountdownLatch和CyclicBarrier的属性



注意:




  • 数量是另一种说不同线程的方法

  • 不可重复使用意味着您必须创建$ b的新实例$ b障碍重用之前

  • 如果线程可以到达并继续工作,障碍是可以推进的
    不等待别人或等待所有线程完成



CountdownLatch




  • 固定数量的参与者

  • 不可重复使用

  • 提前(查看 latch.countDown / code> 可促进
    latch.await(); 必须等待 >


CyclicBarrier




  • 固定会员数

  • 可重复使用

  • 不可升级



所以CountdownLatch是不可重用的,你必须每次创建一个新的实例,但是是可行的。 CylicBarrier可以重复使用,但所有主题必须等待每一方到达屏障。



Phaser / p>


  • 动态参与人数

  • 可重复使用

  • / li>


当线程想要被Phaser知道时,他们调用 phaser.register c>当线程到达屏障时,它们调用 phaser.arrive() ,这里是可行的。如果线程想等待所有注册的任务完成 phaser.arriveAndAwaitAdvance()



还有一个概念其中线程可以在可能尚未完成的其他操作的完成上等待的阶段。一旦所有线程都到达移相器的屏障,则创建新的相位(增量为1)。



您可以查看我的其他答案,也许会有所帮助:



Java ExecutorService:awaitTermination所有递归创建的任务



弹性CountDownLatch?


This question was asked two years ago, but the resources it mentions are either not very helpful (IMHO) or links are no longer valid.

There must be some good tutorials out there to understand Phaser. I've read the javadoc, but my eyes glaze over, since in order to really understand the javadoc you kind of have to know how these classes are supposed to be used.

Anyone have any suggestions?

解决方案

For Phaser I have answered a few questions. Seeing them may help in understanding their applications. They are linked at the bottom. But to understand what the Phaser does and why its useful its important to know what it solves.

Here are attributes of a CountdownLatch and CyclicBarrier

Note:

  • Number of parties is another way of saying # of different threads
  • Not Reusable means you will have to create a new instance of the barrier before reusing
  • A barrier is advanceable if a thread can arrive and continue doing work without waiting for others or can wait for all threads to complete

CountdownLatch

  • Fixed number of parties
  • Not resuable
  • Advanceable (look at latch.countDown(); advanceable latch.await(); must wait )

CyclicBarrier

  • Fixed number of parties
  • Reusable
  • Not advanceable

So the CountdownLatch is not reusable, you must create a new instance each time, but is avanceable. CylicBarrier can be re used but all threads must wait for each party to arrive at the barrier.

Phaser

  • Dynamic number of parties
  • Reusable
  • Advanceable

When a thread wants to be known to the Phaser they invoke phaser.register() when the thread arrives at the barrier they invoke phaser.arrive() and here is where it is advanceable. If the thread wants to await for all registered tasks to complete phaser.arriveAndAwaitAdvance()

There is also the concept of a phase in which threads can wait on a completion of a other operations that may have not completed. Once all threads arrive at the phaser's barrier a new phase is created (an increment of one).

You can take a look at my other answers, maybe it will help:

Java ExecutorService: awaitTermination of all recursively created tasks

Flexible CountDownLatch?

这篇关于Java:jsr166y Phaser的教程/解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆