Phaser和CyclicBarrier之间的区别 [英] Difference between Phaser and CyclicBarrier

查看:178
本文介绍了Phaser和CyclicBarrier之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了Java并发包中CyclicBarrier和Phaser实用程序之间的差异的疑问.

I stumbled upon a doubt regarding a difference between CyclicBarrier and Phaser utilities in Java concurrent package.

我知道CyclicBarrier允许线程组等待,直到所有线程到达特定点为止.移相器也可以这样做,但是它支持多个阶段. 我也了解,可以重用 CyclicBarrier..我认为该重用工具使其功能与Phaser 相同.

I understand that CyclicBarrier allows group of threads to wait until all threads arrive at a specific point. Phaser also do same but it supports multiple phases. I also understand that, a CyclicBarrier can be reused. I think this reuse facility makes its function same as Phaser.

考虑以下程序:

测试阶段工具:

import java.util.concurrent.Phaser;

public class PhaserTest {

    public static void main(String[] args) {
        Phaser p = new Phaser(3);
        Thread t1 = new Thread(() -> process(p), "T1");
        Thread t2 = new Thread(() -> process(p), "T2");
        Thread t3 = new Thread(() -> process(p), "T3");
        t1.start();
        t2.start();
        t3.start();
    }

    private static void process(Phaser p) {
        try {
            System.out.println("Started Phase 1: "+Thread.currentThread().getName());
            p.arriveAndAwaitAdvance();
            System.out.println("Finished Phase 1: "+Thread.currentThread().getName());
            System.out.println("Started Phase 2: "+Thread.currentThread().getName());
            p.arriveAndAwaitAdvance();
            System.out.println("Finished Phase 2: "+Thread.currentThread().getName());
        } catch(Exception e) {}
    }

}

输出:

Started Phase 1: T1
Started Phase 1: T2
Started Phase 1: T3
Finished Phase 1: T3
Started Phase 2: T3
Finished Phase 1: T1
Finished Phase 1: T2
Started Phase 2: T2
Started Phase 2: T1
Finished Phase 2: T2
Finished Phase 2: T3
Finished Phase 2: T1

测试CyclicBarrier:

import java.util.concurrent.CyclicBarrier;


public class CyclicBarrierTest {
    public static void main(String[] args) {    
        CyclicBarrier cb = new CyclicBarrier(3);
        Thread t1 = new Thread(() -> process(cb), "T1");
        Thread t2 = new Thread(() -> process(cb), "T2");
        Thread t3 = new Thread(() -> process(cb), "T3");
        t1.start();
        t2.start();
        t3.start();
    }

    private static void process(CyclicBarrier cb) {
        try {
            System.out.println("Started Phase 1: "+Thread.currentThread().getName());
            cb.await();
            System.out.println("Finished Phase 1: "+Thread.currentThread().getName());
            System.out.println("Started Phase 2: "+Thread.currentThread().getName());
            cb.await();
            System.out.println("Finished Phase 2: "+Thread.currentThread().getName());
        } catch(Exception e) {}
    }
}

输出:

Started Phase 1: T1
Started Phase 1: T2
Started Phase 1: T3
Finished Phase 1: T3
Started Phase 2: T3
Finished Phase 1: T1
Started Phase 2: T1
Finished Phase 1: T2
Started Phase 2: T2
Finished Phase 2: T2
Finished Phase 2: T3
Finished Phase 2: T1

在PhaserTest和CyclicBarrierTest中,直到所有各方都到达/完成上一个阶段后,下一个阶段才开始.

In PhaserTest and CyclicBarrierTest, next phase is not started until all parties arrive/completes the previous phase.

那么,使用Phaser有什么好处?

推荐答案

Phaser来自 JSR-166

在将相位器与现有Java功能进行比较时,解释为 支持与CyclicBarrier类类似的功能 (在Java 5中引入),但是相位器本质上更加灵活:

When comparing phasers to existing Java features, it is explained that similar functionality to the CyclicBarrier class is supported (introduced in Java 5), but phasers are inherently more flexible:

[java.util.concurrent] CyclicBarrier类支持定期屏障 一组线程之间的同步.但是,与相位器不同, CyclicBarriers不支持动态添加或删除 线程;它们也不支持单向同步或分相 手术.探索其他障碍的主要动机之一 实现不仅具有更大的灵活性,而且还具有 提高屏障同步的性能和可伸缩性 概念:

The [java.util.concurrent] CyclicBarrier class supports periodic barrier synchronization among a set of threads. Unlike Phasers, however, CyclicBarriers do not support the dynamic addition or removal of threads; nor do they support one-way synchronization or split-phase operation. One of the main motivators for exploring additional barrier implementations was not just to have increased flexibility, but also to increase performance and scalability of the barrier synchronization concept:

从可移植实现中获得的性能结果 三种不同SMP平台上的移相器的展示表明它们可以 为现有的屏障实施提供卓越的性能 除了他们的生产力所带来的收益 通用性和安全性.

Performance results obtained from a portable implementation of phasers on three different SMP platforms demonstrate that they can deliver superior performance to existing barrier implementations, in addition to the productivity benefits that result from their generality and safety properties.

此处有完整说明: https://www.infoq.com/news/2008/07/phasers

还有另一篇将Phaser定义为CyclicBarier+CountdownLatch的比较文章: http://flex4java. blogspot.com/2015/03/why-and-how-to-to-use-phase-in-java.html

Also another comparing article defining Phaser as CyclicBarier+CountdownLatch: http://flex4java.blogspot.com/2015/03/why-and-how-to-use-phaser-in-java.html

这篇关于Phaser和CyclicBarrier之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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