ConcurrentITeye - AG环亚娱乐

ConcurrentITeye

2019-01-11 11:28:46 | 作者: 昆锐 | 标签: 办法,运转,屏障 | 浏览: 2096

原创转载请注明出处:

 

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 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并发编程中心办法与结构  

 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章