---
title: "Concurrent - Phaser - getPhase() & onAdvance(int phase, int registeredParties)"
author: "fairy_xzc"
date: "2016-12-13T11:50:58Z"
tags: []
categories: ["编程语言"]
slug: "concurrent-phaser-getphase-onadvance-int-phase-int-registeredparties"
draft: false
lastmod: "2016-12-13T11:58:15Z"
id: 2344616
url: "https://agilestyle.iteye.com/blog/2344616"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2344616](/blog/2344616)**

 

## getPhase()

getPhase()作用是获取已经到达第几个屏障

![](http://dl2.iteye.com/upload/attachment/0121/9361/82468f6d-6d6e-3e75-8fd5-dea5a0dcb1d9.png)  
PhaserTest4.java

```
package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest4 {
    public static class MyThread implements Runnable {
        private Phaser phaser;

        public MyThread(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            printPhaseValue();

            printPhaseValue();

            printPhaseValue();

            printPhaseValue();
        }

        private void printPhaseValue() {
            System.out.println("begin...");

            phaser.arriveAndAwaitAdvance();

            System.out.println("end... phase value=" + phaser.getPhase());
        }
    }

    public static void main(String[] args) {
        Phaser phaser = new Phaser(1);
        Thread thread = new Thread(new MyThread(phaser));
        thread.start();
    }
}
```

Run

![](http://dl2.iteye.com/upload/attachment/0121/9363/ce60921b-d48b-395a-bc3d-969962529271.png)  
 

## onAdvance(int phase, int registeredParties)

onAdvance(int phase, int registeredParties)作用是通过新的屏障时被调用

![](http://dl2.iteye.com/upload/attachment/0121/9337/b358cbcb-d03c-3b47-8078-e3a20f75d2d5.png)  
PhaserTest5.java

```
package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest5 {
    public static class Service {
        private Phaser phaser;

        public Service(Phaser phaser) {
            this.phaser = phaser;
        }

        public void testMethod() {
            printPhaseValue();

            printPhaseValue();

            printPhaseValue();

            printPhaseValue();
        }

        private void printPhaseValue() {
            try {
                System.out.println("begin..." + Thread.currentThread().getName());

                if(Thread.currentThread().getName().equals("B")) {
                    Thread.sleep(3000);
                }

                phaser.arriveAndAwaitAdvance();

                System.out.println("end... " + Thread.currentThread().getName() + " phase value= " + phaser.getPhase());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class ThreadA implements Runnable {
        private Service service;

        public ThreadA(Service service) {
            this.service = service;
        }

        @Override
        public void run() {
            service.testMethod();
        }
    }

    public static class ThreadB implements Runnable {
        private Service service;

        public ThreadB(Service service) {
            this.service = service;
        }

        @Override
        public void run() {
            service.testMethod();
        }
    }

    public static void main(String[] args) {
        Phaser phaser = new Phaser(2) {
            @Override
            protected boolean onAdvance(int phase, int registeredParties) {
                System.out.println("onAdvance invoked...");
                return true;
                //return false;
            }
        };

        Service service = new Service(phaser);

        Thread t1 = new Thread(new ThreadA(service));
        t1.setName("A");
        t1.start();

        Thread t2 = new Thread(new ThreadB(service));
        t2.setName("B");
        t2.start();
    }
}
```

Run

![](http://dl2.iteye.com/upload/attachment/0121/9366/5587bc12-6310-3eab-ac8b-c9ffa0de6291.png)  
Note:

```
Phaser phaser = new Phaser(2) {
	@Override
	protected boolean onAdvance(int phase, int registeredParties) {
		System.out.println("onAdvance invoked...");
		return true;
		//return false;
	}
};
```

返回true，表示屏障功能被取消；

注释掉return true; 改为return false; 再Run

![](http://dl2.iteye.com/upload/attachment/0121/9370/e517bdec-c233-3e57-b5a3-85fdc82b0bc5.png)  
Note:

返回false，表示屏障功能继续使用

 

## Reference

Java并发编程核心方法与框架