ITeye博客

Concurrent - Phaser - getPhase() & onAdvance(int phase, int registeredParties)

fairy_xzc · 2016-12-13 11:50 · Concurrent · 编程语言 · Markdown

原创转载请注明出处:http://agilestyle.iteye.com/blog/2344616

 

getPhase()

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


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


 

onAdvance(int phase, int registeredParties)

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


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


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


Note:

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

 

Reference

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。