应用场景:1.多线程任务汇总。2.多线程任务阻塞住,等待发令枪响,一起执行。
减法计数器
每次有线程调用,数量-1,当计数器归零,countDownLatch.await()就会被唤醒向下执行。
- import java.util.concurrent.CountDownLatch;
-
- // 计数器
- public class CountDownLatchDemo {
- public static void main(String[] args) throws InterruptedException {
- //总数是6 必须要执行任务的时候再使用
- CountDownLatch countDownLatch = new CountDownLatch(6);
- for (int i = 0; i < 6; i++) {
- new Thread(()->{
- System.out.println(Thread.currentThread().getName() + " Go out");
- countDownLatch.countDown();//数量-1
- },String.valueOf(i)).start();
- }
- countDownLatch.await();
- System.out.println("Close Door");
- }
-
- }
原理:
countDownLatch.countDown(); //数量-1
countDownLatch.await();//等待计数器归零,然后再往下执行
每次线程调用countDown()数量-1,假设计数器变成0,countDownLatch.await()就会被唤醒,继续执行!
应用场景:比如LOL类游戏,满10人一组,开始游戏。
加法计算器
- import java.util.concurrent.BrokenBarrierException;
- 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 temp = i;
- new Thread(()->{
- System.out.println(Thread.currentThread().getName() + "收集了第" + temp + "颗龙珠");
- try {
- cyclicBarrier.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (BrokenBarrierException e) {
- e.printStackTrace();
- }
- }).start();
- }
- }
- }
Semaphore:信号量
抢车位(6辆车,3个停车位)
- 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(()->{
- //acquire() 得到
- try {
- semaphore.acquire();
- System.out.println(Thread.currentThread().getName() + "抢到车位");
- TimeUnit.SECONDS.sleep(2);
- System.out.println(Thread.currentThread().getName() + "离开车位");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }finally {
- //release() 释放
- semaphore.release();
- }
- },String.valueOf(i)).start();
- }
- }
- }
原理:
semaphore.acquire(); //获得,假设已经满了,等待,等待被释放为止!
semaphore.release();//释放,会将当前的信号量释放,然后唤醒等待线程!
作用:
多个共享资源互斥使用!并发限流,控制最大线程数!