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

原创转载请注明出处: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值