目录
4、arrive / arriveAndDeregister / arriveAndAwaitAdvance
5、awaitAdvance / awaitAdvanceInterruptibly / awaitAdvanceInterruptibly
6、forceTermination / isTerminated
7、getArrivedParties / getUnarrivedParties / getRegisteredParties / getPhase
Phaser 是Java7引入一个用于控制任务阶段执行的可重复使用的同步器,包含了CountDownLatch和CyclicBarrier的功能,比他们更加灵活,更加强大,本篇博客就详细探讨该类的使用和
一、使用
1、基本概念
parties:参与线程的个数,跟CountDownLatch或者CyclicBarrier的构造方法的参数的含义是一样的,不同的是这两个只能在构造方法中指定,不能调整,而Phaser提供了调整的方法。
register / deregister : register就是通知Phaser参与等待的线程数增加了,deregister就是通知Phaser参与等待的线程数减少了,然后相应调整parties
arrive / advance:arrive跟CyclicBarrier中到达栅栏是一个意思,当所有parties个线程都arrive了,则触发advance,默认实现下如果此时parties是0,则会终止Phaser,否则将phase加1,同时将未到达线程数从0恢复至parties
phase:表示执行任务的阶段,初始值是0,每一次advance都会将该值加1,最大值是Integer.MAX_VALUE;如果Phaser被终止了,则该值为负数,此时所有的register,arrive或者await操作都会立即返回。
父子Phaser:父子Phaser一方面可以避免parties线程过多时导致cas修改state容易失败,另一方面可以基于父子Phaser实现复杂的执行任务的阶段控制。子Phaser的parties线程可以有多个,但是对于父Phaser相当于只有一个,只有子Phaser所有的parties线程都到达的时候才通知父Phaser当前子Phaser已到达,只有子Phaser所有的parties线程都被注销(deregister)了才会向父Phaser注销当前子Phaser。另外在多级父子Phaser下,子Phaser的phase永远以上一级的Phaser的phase为准,如果不一致则修改成一致,并且所有的Phaser的root属性都指向同一个祖先Phaser,调用internalAwaitAdvance方法时也是在该Phaser上调用,即所有的子Phaser都共享祖先Phaser的等待线程链表,从而实现最后一个到达的子Phaser可以唤醒其他子Phaser关联的等待线程。
2、实现CountDownLatch的效果
测试用例如下:
@Test
public void test() throws Exception {
int num=6;
Phaser phaser=new Phaser(num);
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
//表示当前线程已到达
phaser.arrive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num;i++){
new Thread(task).start();
}
System.out.println("main thread start await,time->"+System.currentTimeMillis());
//等待其他线程都到达
phaser.awaitAdvance(phaser.getPhase());
System.out.println("main thread end,time->"+System.currentTimeMillis());
}
@Test
public void test2() throws Exception {
int num=6;
CountDownLatch countDownLatch=new CountDownLatch(num);
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num;i++){
new Thread(task).start();
}
System.out.println("main thread start await,time->"+System.currentTimeMillis());
countDownLatch.await();
System.out.println("main thread end,time->"+System.currentTimeMillis());
}
上述测试用例的输出是一样的,主线程等待5个子线程执行完任务然后退出,如下:
3、实现CyclicBarrier的效果
测试用例如下:
@Test
public void test5() throws Exception {
int num=6;
Phaser phaser=new Phaser(num);
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
//到达并等待其他线程到达
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName()+" start,time->"+System.currentTimeMillis());
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
//通知当前线程已到达
phaser.arrive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num-1;i++){
new Thread(task).start();
}
System.out.println("main thread start await,time->"+System.currentTimeMillis());
phaser.awaitAdvance(phaser.getPhase());
System.out.println("all thread start,time->"+System.currentTimeMillis());
phaser.awaitAdvance(phaser.getPhase());
System.out.println("main thread end,time->"+System.currentTimeMillis());
}
@Test
public void test6() throws Exception {
int num=6;
CyclicBarrier cyclicBarrier=new CyclicBarrier(num);
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName()+" start,time->"+System.currentTimeMillis());
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num-1;i++){
new Thread(task).start();
}
System.out.println("main thread start await,time->"+System.currentTimeMillis());
cyclicBarrier.await();
System.out.println("all thread start,time->"+System.currentTimeMillis());
cyclicBarrier.await();
System.out.println("main thread end,time->"+System.currentTimeMillis());
}
输出如下:
CyclicBarrier的构造函数还支持传入一个Runnable,最后一个到达的线程会负责执行该Runnable,Phaser也可实现类似的功能,测试用例如下:
@Test
public void test7() throws Exception {
int num=6;
CyclicBarrier cyclicBarrier=new CyclicBarrier(num, new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" last arrive,time->"+System.currentTimeMillis());
}
});
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName()+" start,time->"+System.currentTimeMillis());
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num-1;i++){
new Thread(task).start();
}
System.out.println("main thread start await,time->"+System.currentTimeMillis());
cyclicBarrier.await();
System.out.println("all thread start,time->"+System.currentTimeMillis());
cyclicBarrier.await();
System.out.println("main thread end,time->"+System.currentTimeMillis());
}
@Test
public void test8() throws Exception {
int num=6;
Phaser phaser=new Phaser(num);
Random random=new Random();
Runnable task=new Runnable() {
@Override
public void run() {
try {
//getUnarrivedParties等于1时,当前线程就是最后一个达到的线程
if(phaser.getUnarrivedParties()==1){
System.out.println(Thread.currentThread().getName()+" last arrive,time->"+System.currentTimeMillis());
}
phaser.arriveAndAwaitAdvance();
System.out.println(Thread.currentThread().getName()+" start,time->"+System.currentTimeMillis());
Thread.sleep(random.nextInt(1000));
System.out.println(Thread.currentThread().getName()+" exit,time->"+System.currentTimeMillis());
if(phaser.getUnarrivedParties()==1){
System.out.println(Thread.currentThread().getName()+" last arrive,time->"+System.currentTimeMillis());
}
phaser.arrive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for(int i=0;i<num;i++){
new Thread(task).s