`

Concurrent - Phaser - forceTermination() & isTerminated()

 
阅读更多

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

 

forceTermination()

forceTermination()的作用是使Phaser对象的屏障功能失效


 

isTerminated()

isTerminated()的作用是判断Phaser对象是否已经呈销毁状态


 

PhaserTest18.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest18 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

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

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

            phaser.arriveAndAwaitAdvance();

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

    public static class ThreadB implements Runnable {
        private Phaser phaser;

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

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

            phaser.arriveAndAwaitAdvance();

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

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

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

        Thread t2 = new Thread(new ThreadB(phaser));
        t2.setName("ThreadB");
        t2.start();
    }
}

Run


Note:

ThreadA和ThreadB呈阻塞状态,因为计数未达到3

 

修改main方法

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

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

		Thread t2 = new Thread(new ThreadB(phaser));
		t2.setName("ThreadB");
		t2.start();

		Thread.sleep(1000);

		phaser.forceTermination();

		System.out.println(phaser.isTerminated());
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

再Run


Note:

forceTermination()仅仅将屏障取消,线程继续执行后面的代码,并不出现异常,而CyclicBarrier的reset()方法执行时会出现异常。

 

控制Phaser类的运行时机

控制线程一起到达屏障后不允许继续运行

PhaserTest20.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

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

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

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

            phaser.arriveAndAwaitAdvance();

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

    public static void main(String[] args) {
        try {
            Phaser phaser = new Phaser(3);
            System.out.println(phaser.getRegisteredParties());

            phaser.register();

            System.out.println(phaser.getRegisteredParties());

            for (int i = 0; i < 3; i++) {
                Thread t = new Thread(new MyThread(phaser));
                t.start();
            }

            Thread.sleep(5000);
            System.out.println("wait for 5 seconds...");
            phaser.arriveAndDeregister();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Run


Note:

本例说明Phaser类运行的时机是可以逻辑控制的,主要的原理就是计数+1,然后通过逻辑代码的方式来决定线程是否继续向下运行。 

 

Reference

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

 

 

 

  • 大小: 3.4 KB
  • 大小: 3.8 KB
  • 大小: 10.6 KB
  • 大小: 16.7 KB
  • 大小: 21.4 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...

    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 文件 , 在 &lt;runtime&gt; 中添加 &lt;library name="lib/geronimo-stax-...

    Android代码-Concurrent-Utils

    This is a library contains some useful and smart utility class for Java concurrent library. Shelly, HermesEventBus and AndroidDataStorage are using this library. Gradle compile 'xiaofei.library:...

    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等闻名,也提供了许多开源工具来支持...

    backport-util-concurrent(2.2 /3.1)

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

    concurrent-all-in-one.pdf

    - 线程状态包括NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING和TERMINATED。 8. **ThreadLocal** - 存储线程局部变量,每个线程有自己的副本,避免数据冲突。 9. **原子类** - 面对多线程环境,提供无锁的...

    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