CountDownLatch
CountDownLatch
内部维护了一个计数器,只有当计数器==0时,某些线程才会停止阻塞,开始执行。
CountDownLatch
主要有两个方法,countDown()
来让计数器减一(-1),await()
来让线程阻塞。当count==0
时,阻塞线程自动唤醒。
案例一班长关门:main线程是班长,6个线程是学生。只有6个线程运行完毕,都离开教室后,main线程班长才会关教室门。
案例二秦灭六国:只有6国都被灭亡后(执行完毕),main线程才会显示“秦国一统天下”。
枚举类的使用
在案例二中会使用到枚举类,因为灭六国,循环6次,想根据i
的值来确定输出什么国,比如1代表楚国,2代表赵国。如果用判断则十分繁杂,而枚举类可以简化操作。
枚举类就像一个简化的数据库,枚举类名就像数据库名,枚举的项目就像数据表,枚举的属性就像表的字段。
关于CountDownLatch
和枚举类的使用,参考示例类:
import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
leaveClassroom();
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();
}, CountryEnum.list(i).getRetMsg()).start();
}
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"\t ******秦国一统华夏");
}
private static void leaveClassroom() 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();
},String.valueOf(i)).start();
}
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"\t ******班长最后关门走人");
}
}
枚举类:
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;
}
}
CyclicBarrier
CountDownLatch
是减,而CyclicBarrier
是加,理解了CountDownLatch
,CyclicBarrier
就很容易。比如召集7颗龙珠才能召唤神龙。
参考类:
import java.util.concurrent.CyclicBarrier;
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();
}
}
}
Semaphore
CountDownLatch
的问题是不能复用。比如count=3
,那么加到3,就不能继续操作了。而Semaphore
可以解决这个问题,比如6辆车3个停车位,对于CountDownLatch
只能停3辆车,而Semaphore
可以停6辆车,车位空出来后,其它车可以占有,这就涉及到了Semaphore.accquire()
和Semaphore.release()
方法。
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
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();
}
}
}