コンカレントコアフレーム:3.Phaserの使用
35991 ワード
CyclicBarrierの欠点:1.partiesカウントを動的に追加することはできません.awaitメソッドを呼び出すにはpartiesカウントが1つしかかかりません
1.Phaserスタックカウントの操作は加算操作
1.Phaserスタックカウントの操作は加算操作
2.arriveAndAwaitAdvance()メソッドテスト
この方法を実行する役割は,現在のスレッドが障壁に達していることであり,ここでしばらく待機し,待機条件が満たされた後も次の障壁に向かってPhaserを実行し続けることは,マルチ障壁を設ける機能を持つ.public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodA();
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodA();
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
PrintTools.phaser = phaser;
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
}
}
:
A A1 begin=1473729059256
B A1 begin=1473729059256
C A1 begin=1473729059258
C A1 end=1473729064258
C A2 begin=1473729064258
B A1 end=1473729064258
B A2 begin=1473729064258
A A1 end=1473729064258
A A2 begin=1473729064258
C A2 end=1473729069258
A A2 end=1473729069258
B A2 end=1473729069258
3.arriveAndAwaitAdvance()メソッドテスト2
上のコードを変更public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729358613
B A1 begin=1473729358613
C A1 begin=1473729358614
B A1 end=1473729363615
C A1 end=1473729363615
B A2 begin=1473729363615
A A1 end=1473729363615
A A2 begin=1473729363615
4.arriveAndDeregisterメソッドテスト public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
System.out.println("A:" + phaser.getRegisteredParties());
phaser.arriveAndDeregister();
System.out.println("B:" + phaser.getRegisteredParties());
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729632834
B A1 begin=1473729632834
C A1 begin=1473729632835
A:3
B:2
C A1 end=1473729637835
B A1 end=1473729637835
A A1 end=1473729637835
A A2 begin=1473729637835
B A2 begin=1473729637835
A A2 end=1473729637835
B A2 end=1473729637835
5 getPhaseとonAdvanceメソッド
5.1 getPhaseメソッドの役割:到着したバリアを取得する
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(1);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
A begin
A end phase value=1
A begin
A end phase value=2
A begin
A end phase value=3
A begin
A end phase value=4
コードの変更public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
a begin
5.2 onAdvanceは新しいバリアを通過する時に呼び出される
public class MyService {
private Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethod() {
try {
System.out.println("A begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("A end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("B begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("B end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("C begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("C end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out .println(" onAdvance !");
return false;
// true ,Phaser /
// false Phaser
}
};
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
}
}
:
A begin ThreadName=A 1473747797710
A begin ThreadName=B 1473747797711
onAdvance !
A end ThreadName=B end phase value=1 1473747802712
A end ThreadName=A end phase value=1 1473747802712
B begin ThreadName=B 1473747802712
B begin ThreadName=A 1473747802712
onAdvance !
B end ThreadName=B end phase value=2 1473747807712
B end ThreadName=A end phase value=2 1473747807712
C begin ThreadName=A 1473747807712
C begin ThreadName=B 1473747807712
onAdvance !
C end ThreadName=B end phase value=3 1473747812712
C end ThreadName=A end phase value=3 1473747812712
6 getRegisterPartiesとregisterメソッド
getRegisterParties登録されたparties数registerを取得する:parties値を動的に追加するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(5);
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
}
}
:
5
6
7
8
9
7.bulkRegisterメソッド
ロット増加parties数public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
}
}
:
10
20
30
40
50
8.getArrivedPartiesおよびgetUnArrivedPartiesメソッド
getArrivedParties使用済みparties数getUnarrivedParites使用されていないparties数を取得するpublic class MyThread extends Thread {
private Phaser phaser;
public MyThread(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(7);
MyThread[] myThreadArray = new MyThread[5];
for (int i = 0; i < myThreadArray.length; i++) {
myThreadArray[i] = new MyThread(phaser);
myThreadArray[i].setName("Thread" + (i + 1));
myThreadArray[i].start();
}
Thread.sleep(2000);
System.out.println(" :" + phaser.getArrivedParties());
System.out.println(" :" + phaser.getUnarrivedParties());
}
}
:
Thread1 A1 begin=1473748522279
Thread2 A1 begin=1473748522279
Thread5 A1 begin=1473748522280
Thread4 A1 begin=1473748522280
Thread3 A1 begin=1473748522280
:5
:2
9.arriveメソッドテスト1
Arriveメソッドの役割:parties値に1を加算し、バリアで待たずに下のコードに直接実行し続け、Phaserはカウントリセット機能を有するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
10.arriveメソッドテスト2 public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
この方法を実行する役割は,現在のスレッドが障壁に達していることであり,ここでしばらく待機し,待機条件が満たされた後も次の障壁に向かってPhaserを実行し続けることは,マルチ障壁を設ける機能を持つ.
public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodA();
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodA();
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
PrintTools.methodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
PrintTools.phaser = phaser;
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
}
}
:
A A1 begin=1473729059256
B A1 begin=1473729059256
C A1 begin=1473729059258
C A1 end=1473729064258
C A2 begin=1473729064258
B A1 end=1473729064258
B A2 begin=1473729064258
A A1 end=1473729064258
A A2 begin=1473729064258
C A2 end=1473729069258
A A2 end=1473729069258
B A2 end=1473729069258
3.arriveAndAwaitAdvance()メソッドテスト2
上のコードを変更public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729358613
B A1 begin=1473729358613
C A1 begin=1473729358614
B A1 end=1473729363615
C A1 end=1473729363615
B A2 begin=1473729363615
A A1 end=1473729363615
A A2 begin=1473729363615
4.arriveAndDeregisterメソッドテスト public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
System.out.println("A:" + phaser.getRegisteredParties());
phaser.arriveAndDeregister();
System.out.println("B:" + phaser.getRegisteredParties());
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729632834
B A1 begin=1473729632834
C A1 begin=1473729632835
A:3
B:2
C A1 end=1473729637835
B A1 end=1473729637835
A A1 end=1473729637835
A A2 begin=1473729637835
B A2 begin=1473729637835
A A2 end=1473729637835
B A2 end=1473729637835
5 getPhaseとonAdvanceメソッド
5.1 getPhaseメソッドの役割:到着したバリアを取得する
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(1);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
A begin
A end phase value=1
A begin
A end phase value=2
A begin
A end phase value=3
A begin
A end phase value=4
コードの変更public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
a begin
5.2 onAdvanceは新しいバリアを通過する時に呼び出される
public class MyService {
private Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethod() {
try {
System.out.println("A begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("A end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("B begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("B end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("C begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("C end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out .println(" onAdvance !");
return false;
// true ,Phaser /
// false Phaser
}
};
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
}
}
:
A begin ThreadName=A 1473747797710
A begin ThreadName=B 1473747797711
onAdvance !
A end ThreadName=B end phase value=1 1473747802712
A end ThreadName=A end phase value=1 1473747802712
B begin ThreadName=B 1473747802712
B begin ThreadName=A 1473747802712
onAdvance !
B end ThreadName=B end phase value=2 1473747807712
B end ThreadName=A end phase value=2 1473747807712
C begin ThreadName=A 1473747807712
C begin ThreadName=B 1473747807712
onAdvance !
C end ThreadName=B end phase value=3 1473747812712
C end ThreadName=A end phase value=3 1473747812712
6 getRegisterPartiesとregisterメソッド
getRegisterParties登録されたparties数registerを取得する:parties値を動的に追加するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(5);
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
}
}
:
5
6
7
8
9
7.bulkRegisterメソッド
ロット増加parties数public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
}
}
:
10
20
30
40
50
8.getArrivedPartiesおよびgetUnArrivedPartiesメソッド
getArrivedParties使用済みparties数getUnarrivedParites使用されていないparties数を取得するpublic class MyThread extends Thread {
private Phaser phaser;
public MyThread(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(7);
MyThread[] myThreadArray = new MyThread[5];
for (int i = 0; i < myThreadArray.length; i++) {
myThreadArray[i] = new MyThread(phaser);
myThreadArray[i].setName("Thread" + (i + 1));
myThreadArray[i].start();
}
Thread.sleep(2000);
System.out.println(" :" + phaser.getArrivedParties());
System.out.println(" :" + phaser.getUnarrivedParties());
}
}
:
Thread1 A1 begin=1473748522279
Thread2 A1 begin=1473748522279
Thread5 A1 begin=1473748522280
Thread4 A1 begin=1473748522280
Thread3 A1 begin=1473748522280
:5
:2
9.arriveメソッドテスト1
Arriveメソッドの役割:parties値に1を加算し、バリアで待たずに下のコードに直接実行し続け、Phaserはカウントリセット機能を有するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
10.arriveメソッドテスト2 public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729358613
B A1 begin=1473729358613
C A1 begin=1473729358614
B A1 end=1473729363615
C A1 end=1473729363615
B A2 begin=1473729363615
A A1 end=1473729363615
A A2 begin=1473729363615
public class PrintTools {
public static Phaser phaser;
public static void methodA() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " A2 begin="
+ System.currentTimeMillis());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A2 end="
+ System.currentTimeMillis());
}
public static void methodB() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
Thread.sleep(5000);
System.out.println("A:" + phaser.getRegisteredParties());
phaser.arriveAndDeregister();
System.out.println("B:" + phaser.getRegisteredParties());
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
A A1 begin=1473729632834
B A1 begin=1473729632834
C A1 begin=1473729632835
A:3
B:2
C A1 end=1473729637835
B A1 end=1473729637835
A A1 end=1473729637835
A A2 begin=1473729637835
B A2 begin=1473729637835
A A2 end=1473729637835
B A2 end=1473729637835
5 getPhaseとonAdvanceメソッド
5.1 getPhaseメソッドの役割:到着したバリアを取得する
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(1);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
A begin
A end phase value=1
A begin
A end phase value=2
A begin
A end phase value=3
A begin
A end phase value=4
コードの変更public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
a begin
5.2 onAdvanceは新しいバリアを通過する時に呼び出される
public class MyService {
private Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethod() {
try {
System.out.println("A begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("A end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("B begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("B end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("C begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("C end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out .println(" onAdvance !");
return false;
// true ,Phaser /
// false Phaser
}
};
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
}
}
:
A begin ThreadName=A 1473747797710
A begin ThreadName=B 1473747797711
onAdvance !
A end ThreadName=B end phase value=1 1473747802712
A end ThreadName=A end phase value=1 1473747802712
B begin ThreadName=B 1473747802712
B begin ThreadName=A 1473747802712
onAdvance !
B end ThreadName=B end phase value=2 1473747807712
B end ThreadName=A end phase value=2 1473747807712
C begin ThreadName=A 1473747807712
C begin ThreadName=B 1473747807712
onAdvance !
C end ThreadName=B end phase value=3 1473747812712
C end ThreadName=A end phase value=3 1473747812712
6 getRegisterPartiesとregisterメソッド
getRegisterParties登録されたparties数registerを取得する:parties値を動的に追加するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(5);
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
}
}
:
5
6
7
8
9
7.bulkRegisterメソッド
ロット増加parties数public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
}
}
:
10
20
30
40
50
8.getArrivedPartiesおよびgetUnArrivedPartiesメソッド
getArrivedParties使用済みparties数getUnarrivedParites使用されていないparties数を取得するpublic class MyThread extends Thread {
private Phaser phaser;
public MyThread(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(7);
MyThread[] myThreadArray = new MyThread[5];
for (int i = 0; i < myThreadArray.length; i++) {
myThreadArray[i] = new MyThread(phaser);
myThreadArray[i].setName("Thread" + (i + 1));
myThreadArray[i].start();
}
Thread.sleep(2000);
System.out.println(" :" + phaser.getArrivedParties());
System.out.println(" :" + phaser.getUnarrivedParties());
}
}
:
Thread1 A1 begin=1473748522279
Thread2 A1 begin=1473748522279
Thread5 A1 begin=1473748522280
Thread4 A1 begin=1473748522280
Thread3 A1 begin=1473748522280
:5
:2
9.arriveメソッドテスト1
Arriveメソッドの役割:parties値に1を加算し、バリアで待たずに下のコードに直接実行し続け、Phaserはカウントリセット機能を有するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
10.arriveメソッドテスト2 public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
System.out.println("A begin");
phaser.arriveAndAwaitAdvance();
System.out.println("A end phase value=" + phaser.getPhase());
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(1);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
A begin
A end phase value=1
A begin
A end phase value=2
A begin
A end phase value=3
A begin
A end phase value=4
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
ThreadA a = new ThreadA(phaser);
a.start();
}
}
:
a begin
public class MyService {
private Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethod() {
try {
System.out.println("A begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("A end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("B begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("B end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
// ////////
System.out.println("C begin ThreadName="
+ Thread.currentThread().getName()
+ " "
+ System.currentTimeMillis());
if (Thread.currentThread().getName().equals("B")) {
Thread.sleep(5000);
}
phaser.arriveAndAwaitAdvance();
System.out.println("C end ThreadName="
+ Thread.currentThread().getName() + " end phase value="
+ phaser.getPhase() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out .println(" onAdvance !");
return false;
// true ,Phaser /
// false Phaser
}
};
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
}
}
:
A begin ThreadName=A 1473747797710
A begin ThreadName=B 1473747797711
onAdvance !
A end ThreadName=B end phase value=1 1473747802712
A end ThreadName=A end phase value=1 1473747802712
B begin ThreadName=B 1473747802712
B begin ThreadName=A 1473747802712
onAdvance !
B end ThreadName=B end phase value=2 1473747807712
B end ThreadName=A end phase value=2 1473747807712
C begin ThreadName=A 1473747807712
C begin ThreadName=B 1473747807712
onAdvance !
C end ThreadName=B end phase value=3 1473747812712
C end ThreadName=A end phase value=3 1473747812712
getRegisterParties登録されたparties数registerを取得する:parties値を動的に追加する
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(5);
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
phaser.register();
System.out.println(phaser.getRegisteredParties());
}
}
:
5
6
7
8
9
7.bulkRegisterメソッド
ロット増加parties数public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
}
}
:
10
20
30
40
50
8.getArrivedPartiesおよびgetUnArrivedPartiesメソッド
getArrivedParties使用済みparties数getUnarrivedParites使用されていないparties数を取得するpublic class MyThread extends Thread {
private Phaser phaser;
public MyThread(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(7);
MyThread[] myThreadArray = new MyThread[5];
for (int i = 0; i < myThreadArray.length; i++) {
myThreadArray[i] = new MyThread(phaser);
myThreadArray[i].setName("Thread" + (i + 1));
myThreadArray[i].start();
}
Thread.sleep(2000);
System.out.println(" :" + phaser.getArrivedParties());
System.out.println(" :" + phaser.getUnarrivedParties());
}
}
:
Thread1 A1 begin=1473748522279
Thread2 A1 begin=1473748522279
Thread5 A1 begin=1473748522280
Thread4 A1 begin=1473748522280
Thread3 A1 begin=1473748522280
:5
:2
9.arriveメソッドテスト1
Arriveメソッドの役割:parties値に1を加算し、バリアで待たずに下のコードに直接実行し続け、Phaserはカウントリセット機能を有するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
10.arriveメソッドテスト2 public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
phaser.bulkRegister(10);
System.out.println(phaser.getRegisteredParties());
}
}
:
10
20
30
40
50
getArrivedParties使用済みparties数getUnarrivedParites使用されていないparties数を取得する
public class MyThread extends Thread {
private Phaser phaser;
public MyThread(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(7);
MyThread[] myThreadArray = new MyThread[5];
for (int i = 0; i < myThreadArray.length; i++) {
myThreadArray[i] = new MyThread(phaser);
myThreadArray[i].setName("Thread" + (i + 1));
myThreadArray[i].start();
}
Thread.sleep(2000);
System.out.println(" :" + phaser.getArrivedParties());
System.out.println(" :" + phaser.getUnarrivedParties());
}
}
:
Thread1 A1 begin=1473748522279
Thread2 A1 begin=1473748522279
Thread5 A1 begin=1473748522280
Thread4 A1 begin=1473748522280
Thread3 A1 begin=1473748522280
:5
:2
9.arriveメソッドテスト1
Arriveメソッドの役割:parties値に1を加算し、バリアで待たずに下のコードに直接実行し続け、Phaserはカウントリセット機能を有するpublic class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
10.arriveメソッドテスト2 public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(2) {
protected boolean onAdvance(int phase, int registeredParties) {
System.out.println(" !phase=" + phase
+ " registeredParties=" + registeredParties);
return super.onAdvance(phase, registeredParties);
};
};
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("A2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("B2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C1 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
phaser.arrive();
System.out.println("C2 getPhase=" + phaser.getPhase()
+ " getRegisteredParties=" + phaser.getRegisteredParties()
+ " getArrivedParties=" + phaser.getArrivedParties());
// //////////////
}
}
:
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=0
A1 getPhase=0 getRegisteredParties=2 getArrivedParties=1
A2 getPhase=0 getRegisteredParties=2 getArrivedParties=1
!phase=0 registeredParties=2
A2 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=0
B1 getPhase=1 getRegisteredParties=2 getArrivedParties=1
B2 getPhase=1 getRegisteredParties=2 getArrivedParties=1
!phase=1 registeredParties=2
B2 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=0
C1 getPhase=2 getRegisteredParties=2 getArrivedParties=1
C2 getPhase=2 getRegisteredParties=2 getArrivedParties=1
!phase=2 registeredParties=2
C2 getPhase=3 getRegisteredParties=2 getArrivedParties=0
public class MyService {
public Phaser phaser;
public MyService(Phaser phaser) {
super();
this.phaser = phaser;
}
public void testMethodA() {
try {
System.out.println(Thread.currentThread().getName() + " begin A1 "
+ System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(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);
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);
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 class ThreadA extends Thread {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadB extends Thread {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodA();
}
}
public class ThreadC extends Thread {
private MyService myService;
public ThreadC(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.testMethodB();
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
MyService service = new MyService(phaser);
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
ThreadC c = new ThreadC(service);
c.setName("C");
c.start();
}
}
:arrive , AB
A begin A1 1473749324848
B begin A1 1473749324849
C begin A1 1473749324850
C end A1 1473749324850
C begin A2 1473749324850
C end A2 1473749324850
C begin A3 1473749324850
C end A3 1473749324850
0
1
11 awaitAndAdvance(phase)メソッド
awaitAndAdvance(phase):受信したphaseの値が現在のgetPhaseメソッドの戻り値と一致する場合は、バリアで待機します.そうしないと、awaitAndAdvance(phase)を下に実行し続け、カウントに参加せず、判断機能のみを有します.public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
12 awaitAdvanceinterruptibly(int)テスト1
awaitAdvanceinterruptiblyメソッドは中断できませんpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
}
}
public class ThreadC extends Thread {
private Phaser phaser;
public ThreadC(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(3000);
phaser.awaitAdvance(0);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadD extends Thread {
private Phaser phaser;
public ThreadD(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
Thread.sleep(5000);
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis()+" getPhase"+phaser.getPhase());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
ThreadC c = new ThreadC(phaser);
c.setName("C");
c.start();
ThreadD d = new ThreadD(phaser);
d.setName("D");
d.start();
}
}
:
A A1 begin=1473749992110 getPhase0
B A1 begin=1473749992111 getPhase0
C A1 begin=1473749992112 getPhase0
D A1 begin=1473749992113 getPhase0
D A1 end=1473749997114 getPhase1
A A1 end=1473749997114 getPhase1
C A1 end=1473749997114 getPhase1
B A1 end=1473749997114 getPhase1
awaitAdvanceinterruptiblyメソッドは中断できません
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis()+" getPhase "+phaser.getPhase());
phaser.awaitAdvance(0);
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750345591 getPhase 0
c
12 await Advanceinterruptiblyメソッドテスト2 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
14 awaitAdvanceinterruptiblyメソッドテスト3 public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0);// wait
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(5000);
a.interrupt();
System.out.println(" c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473750452441
c
catch
java.lang.InterruptedException
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:18)
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " A1 begin="
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(10);//
System.out.println(Thread.currentThread().getName() + " A1 end="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
System.out.println(" catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A A1 begin=1473750622169
A A1 end=1473750622169
15 await Advanceinterruptibly(phase,timeout,timeunit)メソッドテスト4
指定された時間内に変化がない場合は、例外が発生します.そうでない場合は、下に進みます.
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
16 forceTerminationとisTerminatedメソッド
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断するpublic class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("InterruptedException e");
} catch (TimeoutException e) {
e.printStackTrace();
System.out.println("TimeoutException e");
}
}
}
public class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
}
}
:
A begin 1473750743978
java.util.concurrent.TimeoutExceptionTimeoutException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
Thread.sleep(1000);
phaser.arrive();
System.out.println(System.currentTimeMillis());
}
}
:
A begin 1473750909777
1473750912777
A end 1473750912777
public class Run3 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
:
A begin 1473750949798
java.lang.InterruptedException
InterruptedException e
at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Unknown Source)
at extthread.ThreadA.run(ThreadA.java:21)
forceTerminationの機能:Phaserオブジェクトのバリア機能を無効にするisTerminatedの機能:Phaserオブジェクトが破棄されたかどうかを判断する
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class ThreadB extends Thread {
private Phaser phaser;
public ThreadB(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
}
}
:
A A1 begin=1473751201581
B A1 begin=1473751201583
public class Run2 {
public static void main(String[] args) {
try {
Phaser phaser = new Phaser(3);
ThreadA a = new ThreadA(phaser);
a.setName("A");
a.start();
ThreadB b = new ThreadB(phaser);
b.setName("B");
b.start();
Thread.sleep(1000);
phaser.forceTermination();
System.out.println(phaser.isTerminated());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
:
A A1 begin=1473751225751
B A1 begin=1473751225752
true
A A1 end=1473751226752
B A1 end=1473751226752
17 Phaserクラスの実行タイミングを制御する public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}
public class ThreadA extends Thread {
private Phaser phaser;
public ThreadA(Phaser phaser) {
super();
this.phaser = phaser;
}
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 class Run1 {
public static void main(String[] args) {
Phaser phaser = new Phaser(3);
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Phaser phaser = new Phaser(3);
phaser.register();
for (int i = 0; i < 3; i++) {
ThreadA t = new ThreadA(phaser);
t.start();
}
Thread.sleep(5000);
phaser.arriveAndDeregister();
}
}