`

Concurrent - Phaser - arrive()

 
阅读更多

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

 

arrive()

arrive()作用是使parites值加1,并且不在屏障处等待,直接运行下面的代码,并且Phaser有计数重置功能


PhaserTest8.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

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

        System.out.println("A1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("A1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        System.out.println("A2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("A2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        //--------------------------------------------------------------------------
        System.out.println("B1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("B1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        System.out.println("B2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("B2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        //--------------------------------------------------------------------------
        System.out.println("C1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("C1 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        System.out.println("C2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

        phaser.arrive();

        System.out.println("C2 phaser.getPhase()=" + phaser.getPhase() +
                "\tphaser.getRegisteredParties()=" + phaser.getRegisteredParties() +
                "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());
    }
}

Run


Note:

arrive()功能是使getArrivedParties()计数加1,不等待其他线程到达屏障。

控制台中多次出现phaser.getArrivedParties()=0的运行结果,可以分析出Phaser在经过屏障点后计数被重置。

 

PhaserTest9.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

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

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

        public void testMethodA() {
            try {
                System.out.println(Thread.currentThread().getName() + " begin A1 " + System.currentTimeMillis());

                Thread.sleep(3000);

                System.out.println(Thread.currentThread().getName() + "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

                phaser.arriveAndAwaitAdvance();

                System.out.println(Thread.currentThread().getName() + " end A1 " + System.currentTimeMillis());


                System.out.println(Thread.currentThread().getName() + " begin A2 " + System.currentTimeMillis());

                Thread.sleep(3000);

                System.out.println(Thread.currentThread().getName() + "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

                phaser.arriveAndAwaitAdvance();

                System.out.println(Thread.currentThread().getName() + " end A2 " + System.currentTimeMillis());


                System.out.println(Thread.currentThread().getName() + " begin A3 " + System.currentTimeMillis());

                Thread.sleep(3000);

                System.out.println(Thread.currentThread().getName() + "\tphaser.getArrivedParties()=" + phaser.getArrivedParties());

                phaser.arriveAndAwaitAdvance();

                System.out.println(Thread.currentThread().getName() + " end A3 " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void testMethodB() {
            System.out.println(Thread.currentThread().getName() + " begin A1 " + System.currentTimeMillis());

            phaser.arrive();

            System.out.println(Thread.currentThread().getName() + " end A1 " + System.currentTimeMillis());


            System.out.println(Thread.currentThread().getName() + " begin A2 " + System.currentTimeMillis());

            phaser.arrive();

            System.out.println(Thread.currentThread().getName() + " end A2 " + System.currentTimeMillis());


            System.out.println(Thread.currentThread().getName() + " begin A3 " + System.currentTimeMillis());

            phaser.arrive();

            System.out.println(Thread.currentThread().getName() + " end A3 " + System.currentTimeMillis());
        }
    }

    public static class ThreadA implements Runnable {

        private Service service;

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

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

    public static class ThreadB implements Runnable {

        private Service service;

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

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

    public static class ThreadC implements Runnable {

        private Service service;

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

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

    public static void main(String[] args) {
        Phaser phaser = new Phaser(3);

        Service service = new Service(phaser);

        Thread t1 = new Thread(new ThreadA(service));
        Thread t2 = new Thread(new ThreadB(service));
        Thread t3 = new Thread(new ThreadC(service));

        t1.start();
        t2.start();
        t3.start();
    }
}

Run


Note:

线程2在parties计数达到3后自动重置为0,线程0和1由于达不到parties为3的情况,所以依然呈等待状态。 

 

Reference

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

  • 大小: 3.8 KB
  • 大小: 41.6 KB
  • 大小: 25.3 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-...

    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 , ...

    concurrent-1.3.4.jar

    concurrent-1.3.4.jar

    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...

    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.jar.zip`是这个库的压缩文件,包含了`atlassian-util-concurrent-0.0.12.jar`本身以及相关的许可证文件`atlassian-profiling-license.txt`。这个库是Atlassian为Java开发者提供的...

    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