1.CountDownLatch:(减:倒得计数器)
让一些线程阻塞直到另一个线程完成一系列操作后才被唤醒。
CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,调用线程会被阻塞。当它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞),当计数器的值变为0时,因调用await方法被阻塞的线程会被唤醒,继续执行。
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch downLatch = new CountDownLatch(10);
for (int i = 1; i <= 10; i++) {
new Thread(() ->{
System.out.println(Thread.currentThread().getName()+"\t 上自习,离开教室");
downLatch.countDown();
},String.valueOf(i)).start();
}
downLatch.await();//只有当上面为0的时候,才会执行最下面的这个
System.out.println(Thread.currentThread().getName()+"**************************班长走人了");
}
}
结合枚举使用:
public enum CountryEnum {
ONE(1,"齐"),
TWO(2,"楚"),
THREE(3,"燕"),
FOUR(4,"韩"),
FIVE(5,"赵"),
SIX(6,"魏");
private Integer retcode;
private String retMessage;
public Integer getRetcode() {
return retcode;
}
public void setRetcode(Integer retcode) {
this.retcode = retcode;
}
public String getRetMessage() {
return retMessage;
}
public void setRetMessage(String retMessage) {
this.retMessage = retMessage;
}
CountryEnum(Integer retcode, String retMessage) {
this.retcode = retcode;
this.retMessage = retMessage;
}
public static CountryEnum forEach_CountEnum(int index){
CountryEnum[] values = CountryEnum.values();
for (CountryEnum value : values) {
if(index == value.getRetcode()){
return value;
}
}
return null;
}
}
//枚举相当于一个比较小型的数据库,可以操作数据,同时和map集合也是非常的相似的。可以用来存储数据
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(6);
for (int i = 1; i <= 6; i++) {
new Thread(() ->{
System.out.println(Thread.currentThread().getName()+"\t被灭了");
latch.countDown();
},CountryEnum.forEach_CountEnum(i).getRetMessage()).start();
}
latch.await();
System.out.println("秦国统一了华夏");
}
}
2.CyclicBarrier(加:正的计数器):
CyclicBarrier的字面意思是可循环(Cyclic)使用的屏障(Barrier)。它要做的事情是让一组线程到达一个平常(也可以叫同步点)时被阻塞,直到最后一个线程达到屏障时,屏障才会开门,所有被屏障连接的线程才会继续干活,线程进入到屏障通过CyclicBarrier的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 temp = i;
new Thread(() ->{
System.out.println(Thread.currentThread().getName()+"\t 第"+temp+"个开始了");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
3.Semaphore(并发量控制)
Semaphore:信号量主要用于两个目的,一个是用于多个共享资源的互斥使用,另一个用于并发编程数的控制。
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抢到车位了");
Thread.sleep(300);
System.out.println(Thread.currentThread().getName()+"\t停了几秒钟之后,就走了");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
},String.valueOf(i)).start();
}
}
}