CountDownLatch、CyclicBarrier、Semaphore
CountDownLatch
CountDownLatch倒计时锁(闭锁):是一个同步的工具类,使一个线程阻塞,等待其他线程都运行结束后再被唤醒。与它一同的工具类还有CyclicBarrier、Semaphore、concurrentHashMap和BlockingQueue。
CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,调用线程会被阻塞,其他线程调用countDown方法计数器减1(调用countDown方法时线程不会阻塞),当计数器的值变为0,因调用await方法被阻塞的线程会被唤醒,继续执行。
案例
public enum CountryEnum {
ONE(1, "齐"), TWO(2, "楚"), THREE(3, "燕"), FOUR(4, "赵"), FIVE(5, "魏"), SIX(6, "韩");
private Integer retCode;
private String retMsg;
CountryEnum(Integer retCode, String retMsg) {
this.retCode = retCode;
this.retMsg = retMsg;
}
public Integer getRetCode() {
return retCode;
}
public void setRetCode(Integer retCode) {
this.retCode = retCode;
}
public String getRetMsg() {
return retMsg;
}
public void setRetMsg(String retMsg) {
this.retMsg = retMsg;
}
public static CountryEnum list(int idx) {
CountryEnum[] countryEnums = CountryEnum.values();
for (CountryEnum countryEnum : countryEnums) {
if (idx==countryEnum.getRetCode())
return countryEnum;
}
return null;
}
}
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
county();
}
private static void county() throws InterruptedException {
CountDownLatch countDownLatch=new CountDownLatch(6);
for (int i = 1; i <=6 ; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"\t 国被灭");
countDownLatch.countDown();//计数器-1
}, CountryEnum.list(i).getRetMsg()).start();
}
countDownLatch.await();//挂起等待其他线程运行结束
System.out.println(Thread.currentThread().getName()+"\t ******秦国一统华夏");
}
}
秦国想要一统华夏,那么就得先灭掉六国;秦国就相当于一个线程,需要等其他6个线程运行结束,才能统一。for循环创建6个线程,主线程调用countDownLatch.await()挂起等待其他线程运行结束;其余6个线程都打印一条被灭的国家,打印结束后调用countDownLatch.countDown();将计数器减1;最终6个线程都运行完后计数器就被减为0,那么主线程就会被唤醒,最后打印秦国。
运行结果:
CountDownLatch countDownLatch=new CountDownLatch(6);创建闭锁,并初始化计数器为6;
CyclicBarrier
CyclicBarrier****循环屏障,与CountDownLatch相似,但又不一样。CyclicBarrier的字面意思是可循环(Cyclic) 使用的屏障(barrier)。它要做的事情是,让一组线程到达一个屏障(也可以叫做同步点)时被阻塞,知道最后一个线程到达屏障时,屏障才会开门,所有被屏障拦截的线程才会继续干活,线程进入屏障通过CyclicBarrier的await()方法。
什么意思呢,举个例子,《七龙珠》看过吧,需要集齐七颗龙珠才能召唤神龙。屏障在就是指要满足的条件,屏障在这里指:是否集齐七颗龙珠。每一个线程都会收集一颗龙珠,当一个线程收集到一颗龙珠时会判断是否满足屏障的条件:是否集齐七颗龙珠;如果没有那么该线程就在此等候await(),如果集齐了,就可以执行最后的任务召唤神龙。
构造方法
public CyclicBarrier(int parties)
public CyclicBarrier(int parties, Runnable barrierAction)
parties:表示参与的线程数
barrierAction:突破屏障后最终要执行的任务
await()方法:表示线程到达屏障并在此挂起。
案例
我们就以七龙珠为例子:
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier=new CyclicBarrier(7,()->{
System.out.println("*****召唤神龙");
});
for (int i = 1; i <=7 ; i++) {
final int tempInt=i;
new Thread(()->{
System.out.println(Thread.currentThread().getName()+
"\t 收集到第"+tempInt+"颗龙珠");
try{
cyclicBarrier.await();
}catch (Exception e){
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
首先我们创建了一个循环屏障,要求要7个线程达到屏障才能执行最终的任务(召唤神龙)。for循环创建了7个线程,分别打印收集到的第几颗龙珠数,在没有收集到最后一颗之前都调用await()方法等待挂起;7颗龙珠收集齐之后最后打印召唤神龙。
运行结果:
Semaphore
Semaphore信号量,主要有两个目的,一个是用于多个共享资源的相互排斥使用,另一个用于并发资源数的控制。
比如有3个停车位,那么信号量就用来显示空的停车位数;有6辆车想要在停车场停车,但是只有3个车位;那么有3辆车能够调用acquire()方法获得一个 获得一个令牌,代表有资格使用停车位;那么此时信号量为0,后面来的3辆车只能等待挂起;直到停车位有车位空出,信号量+1,后面的车才能获得令牌停车。
案例
以停车位为例,有3个停车位,6辆车。
public class SemaphoreDemo {
public static void main(String[] args) {
Semaphore semaphore=new Semaphore(3);
for (int i = 1; i <=6 ; i++) {
new Thread(()->{
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+
"\t抢到车位");
try{ TimeUnit.SECONDS.sleep(3); }catch (Exception e){ e.printStackTrace(); }
System.out.println(Thread.currentThread().getName()+
"\t停车3秒后离开车位");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
},String.valueOf(i)).start();
}
}
}
运行结果:
信号量可以控制同时访问特定资源的线程数量,以保证合理的使用资源。
使用场景:
数据库连接池。限制同时连接数据库的线程数量,当连接到达上限时,后面的线程会放入等待队列中等待前面线程是否数据库连接。