本文主要是介绍Java 并发编程学习笔记(9) ----Phaser-arriveAndAwaitAdvice(),arriveAndDeregister(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Phaser 移相器
Phaser具有设置多屏障的功能。
1.方法arriveAndAwaitAdvice()
方法arriveAndAwaitAdvice()的作用与CountDownLatch中的await()方法大体一样。
另一个作用是计数不足时,线程呈阻塞状态,不能继续向下运行。
1
2
21.方法arriveAndDeregister()
使当前线程退出,并且是parties值减1
1
22.代码
package com.lhc.concurrent.phaser.arriveAndDeregister;
import java.util.concurrent.Phaser;
public class DerThread1 extends Thread{
private Phaser phaser;
public DerThread1(Phaser phaser) {
this.phaser = phaser;
}
@Override
public void run() {
PhaserTool.method1();
}
public static void main(String[] args){
Phaser phaser = new Phaser(3);
PhaserTool.phaser = phaser;
DerThread1 derThreadA = new DerThread1(phaser);
derThreadA.setName("A");
derThreadA.start();
DerThread2 derThreadC = new DerThread2(phaser);
derThreadC.setName("C");
derThreadC.start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.lhc.concurrent.phaser.arriveAndDeregister;
import java.util.concurrent.Phaser;
public class DerThread2 extends Thread{
private Phaser phaser;
public DerThread2(Phaser phaser) {
this.phaser = phaser;
}
@Override
public void run() {
PhaserTool.method2();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.lhc.concurrent.phaser.arriveAndDeregister;
import com.lhc.concurrent.exchanger.timeout.ThreadA;
import java.util.concurrent.Phaser;
public class PhaserTool {
public static Phaser phaser;
public static void method1(){
System.out.println(Thread.currentThread().getName() + " 1 begin = " + System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " 1 end = " + System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " 2 begin = " + System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " 2 end = " + System.currentTimeMillis());
}
public static void method2(){
try {
System.out.println(Thread.currentThread().getName() + " 1 begin = " + System.currentTimeMillis());
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " parties: " + phaser.getRegisteredParties());
//取消注册 parties -1
phaser.arriveAndDeregister();
System.out.println(Thread.currentThread().getName() + " 取消注册," + "parties: " + phaser.getRegisteredParties());
//取消注册 parties -1
phaser.arriveAndDeregister();
System.out.println(Thread.currentThread().getName() + " 取消注册," + "parties: " + phaser.getRegisteredParties());
System.out.println(Thread.currentThread().getName() + " 1 end = " + System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
23.输出结果
A 1 begin = 1556525129659
C 1 begin = 1556525129659
C parties: 3
C 取消注册,parties: 2
C 取消注册,parties: 1
C 1 end = 1556525131661
A 1 end = 1556525131661
A 2 begin = 1556525131661
A 2 end = 1556525131661
这篇关于Java 并发编程学习笔记(9) ----Phaser-arriveAndAwaitAdvice(),arriveAndDeregister()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!