`

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并发编程核心方法与框架 

 

  • 大小: 7.6 KB
  • 大小: 2.8 KB
  • 大小: 17.1 KB
  • 大小: 25.5 KB
  • 大小: 24.7 KB
分享到:
评论

相关推荐

    JavaEE源代码 concurrent-1.3.2

    JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-...

    concurrent-1.3.4.jar

    concurrent-1.3.4.jar

    backport-util-concurrent-3.1.jar

    - copy %AXIS2_HOME%\lib\backport-util-concurrent-3.1.jar 到%ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\lib - 注册此 jar 包: 編輯 %ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\plugin.xml , ...

    backport-util-concurrent-3.1.jar geronimo-stax-api_1.0_spec-1.0.1.jar 下载

    backport-util-concurrent-3.1.jar 和 geronimo-stax-api_1.0_spec-1.0.1.jar 复制到 MyEclipse 6.5\eclipse\plugins\Axis2_Codegen_Wizard_1.3.0\lib 文件夹下。 (3).注册此 jar 包: 修改MyEclipse 6.5\eclipse...

    jodconverter-core-3.0-beta-4jar&sources&javadoc

    3. **并发处理**:`org.jodconverter.concurrent`包提供了并发处理工具,允许在多线程环境中高效地执行大量转换任务。 4. **日志记录**:JodConverter集成了SLF4J(Simple Logging Facade for Java),方便开发者...

    backport-util-concurrent-java12-3.1-sources.jar

    官方版本,亲测可用

    concurrent-1.3.4-sources.jar

    《并发编程库 concurrent-1.3.4-sources.jar 深度解析》 在Java编程领域,"concurrent"一词通常与多线程和并发处理相关,它指的是能够同时执行多个任务的能力。这里提到的`concurrent-1.3.4-sources.jar`是一个特定...

    backport-util-concurrent-3.1.jar和geronimo-stax-api_1.0_spec-1.0.1.jar

    "backport-util-concurrent-3.1.jar" 文件到 Axis2_Codegen_Wizard_1.3.0 的 lib 目录中 , 同时修改 Axis2_Codegen_Wizard_1.3.0 下的 plugin.xml 文件 , 在 <runtime> 中添加 <library name="lib/geronimo-stax-...

    Android代码-Concurrent-Utils

    Concurrent-Utils Utilities for Java concurrent library. This is a library contains some useful and smart utility class for Java concurrent library. Shelly, HermesEventBus and AndroidDataStorage are ...

    concurrent-1.3.2.jar

    concurrent-1.3.2.ja

    atlassian-util-concurrent-0.0.12.jar.zip

    《深入解析Atlassian Util Concurrent库:0.0.12版本》 在IT行业中,高效且可靠的并发处理是系统性能优化的关键因素之一。Atlassian公司,以其强大的协作工具如Jira、Confluence等闻名,也提供了许多开源工具来支持...

    concurrent-all-in-one.pdf

    《并发编程全方位解析》 并发编程是现代计算机系统中不可或缺的一部分,尤其是在多核处理器和分布式系统环境下。本章将深入探讨Java并发编程的核心概念、工具和最佳实践。 1. **Java并发组件概述** ...

    backport-util-concurrent(2.2 /3.1)

    - 在3.1版本中,backport-util-concurrent引入了Phaser,这是一个可重用的同步帮助器类,支持多个参与者之间的有界同步,类似于CyclicBarrier和CountDownLatch,但更灵活。Phaser可以自动调整参与者的数量,并且在...

    backport-util-concurrent-1.0.jar

    backport-util-concurrent-1.0.jar,

    patterns-for-concurrent-and-networked-objects

    patterns-for-concurrent-and-networked-objects

Global site tag (gtag.js) - Google Analytics