java并发工具
CountDownLatch
作用:当线程执行一定的数量之后,才继续执行后序代码
CountDownLatch countDownLatch = new CountDownLatch(3);//需要执行的线程数量
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(111);
countDownLatch.countDown();//计数
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(222);
countDownLatch.countDown();//计数
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(333);
countDownLatch.countDown();//计数
}
}).start();
countDownLatch.await();//数量未达到,不往下执行
System.out.println("三个线程执行完毕");
CyclicBarrier
作用:循环栅栏,将线程分组
public static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);//三个为一组
public static void main(String[] args) throws Exception {
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(111);
try {
cyclicBarrier.await();//计数
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(222);
try {
cyclicBarrier.await();//计数
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(333);
try {
cyclicBarrier.await();//计数
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
结果:
222
111
333
333
111
222
222
111
222
333
333
111
333
222
111
Process finished with exit code 0
Semaphore
作用:控制并发数
public static Semaphore semaphore = new Semaphore(3);//最大资源申请数
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();//申请资源
System.out.println(111);
semaphore.release();//释放资源
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();//申请资源
System.out.println(444);
semaphore.release();//释放资源
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();//申请资源
System.out.println(333);
semaphore.release();//释放资源
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Phaser
作用:更灵活的工具
new Phaser(3).register()
动态添加一个或多个参与者,同时返回phase值作抵达分类用。
new Phaser(3).bulkRegister(int parties)
将指定数目的参与者注册到phaser中,所有这些新的参与者都将被当成没有执行完本阶段的线程。
new Phaser(3).arriveAndAwaitAdvance()
类似await()方法,记录到达线程数,阻塞等待其他线程到达同步点后再继续执行。
new Phaser(3).arriveAndDeregister()
动态撤销线程在phaser的注册,通知phaser对象,该线程已经结束该阶段且不参与后面阶段。由此减少了未来phase上需要前进的线程数量。
new Phaser(3).arrive()
通知phaser该线程已经完成该阶段,但不等待其他线程。必须小心使用这个方法,因为它不会与其他线程同步。
new Phaser(3).isTerminated()
phaser是否被终止
Exchanger
作用:两个线程进行数据交换
public static Exchanger<String> stringExchanger = new Exchanger<>();
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
String str = stringExchanger.exchange("111的数据");
System.out.println("111获取到的数据: "+str);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
String str = stringExchanger.exchange("222的数据");
System.out.println("222获取到的数据: "+str);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();//申请资源
System.out.println(333);
semaphore.release();//释放资源
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
结果:
222获取到的数据: 111的数据
111获取到的数据: 222的数据
Process finished with exit code 0