PhaserDemo

package com.smart.thread;

import java.io.IOException;
import java.util.concurrent.Phaser;

/**

  • 一种任务可以分为多个阶段,现希望多个线程去处理该批任务,对于每个阶段,多个线程可以并发进行,

  • 但是希望保证只有前面一个阶段的任务完成之后才能开始后面的任务。

  • 这种场景可以使用多个CyclicBarrier来实现,每个CyclicBarrier负责等待一个阶段的任务全部完成。

  • 但是使用CyclicBarrier的缺点在于,需要明确知道总共有多少个阶段,同时并行的任务数需要提前预定义好,

  • 且无法动态修改。而Phaser可同时解决这两个问题。

  • Created by jinxiaoyu on 17/4/11.
    */
    public class PhaserDemo {
    public static void main(String[] args) throws IOException {
    int parties = 3;
    final int phases = 4;
    final Phaser phaser = new Phaser(parties) {
    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
    System.out.println("====== Phase : " + phase + " ======");
    return registeredParties == 0;
    }
    };

     for(int i = 0; i < parties; i++) {
         final int threadId = i;
         final Thread thread = new Thread() {
             @Override
             public void run() {
                 for (int phase = 0; phase < phases; phase++) {
                     System.out.println(String.format("Thread %s, phase %s", threadId, phase));
                     phaser.arriveAndAwaitAdvance();
                 }
             }
         };
         thread.start();
     }
    

    }
    }

Thread 1, phase 0
Thread 2, phase 0
Thread 0, phase 0
====== Phase : 0 ======
Thread 1, phase 1
Thread 0, phase 1
Thread 2, phase 1
====== Phase : 1 ======
Thread 0, phase 2
Thread 1, phase 2
Thread 2, phase 2
====== Phase : 2 ======
Thread 1, phase 3
Thread 0, phase 3
Thread 2, phase 3
====== Phase : 3 ======

你可能感兴趣的:(PhaserDemo)