7.1 CountDownLatch

让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒
CountDownLatch 主要有两个方法,当一个或多个线程调用 await 方法时,调用线程会被阻塞。其它线程调用 countDown 方法会将计数器减 1(调用 countDown 方法的线程不会阻塞),
当计数器的值变为零时,因调用 await 方法被阻塞的线程会被唤醒,继续执行。

  1. package s02.e07;
  2. public class CountDownLatchDemo {
  3. public static void main(String[] args) throws Exception {
  4. for (int i = 1; i <= 6; i++) {
  5. new Thread(() -> {
  6. System.out.println(Thread.currentThread().getName() + "\t上完自习,离开教室");
  7. }, String.valueOf(i)).start();
  8. }
  9. System.out.println(Thread.currentThread().getName() + "\t*****************班长最后关门走人");
  10. }
  11. }

image.png
不使用 CountDownLatch,main 线程不会最后执行

  1. package s02.e07;
  2. import java.util.concurrent.CountDownLatch;
  3. public class CountDownLatchDemo {
  4. public static void main(String[] args) throws Exception {
  5. CountDownLatch countDownLatch = new CountDownLatch(6);
  6. for (int i = 1; i <= 6; i++) {
  7. new Thread(() -> {
  8. System.out.println(Thread.currentThread().getName() + "\t上完自习,离开教室");
  9. countDownLatch.countDown();
  10. }, String.valueOf(i)).start();
  11. }
  12. countDownLatch.await();
  13. System.out.println(Thread.currentThread().getName() + "\t*****************班长最后关门走人");
  14. }
  15. }

image.png
使用 CountDownLatch 后,main 线程最后执行

  1. package s02.e07;
  2. import lombok.Getter;
  3. public enum CountryEnum {
  4. ONE(1, "齐"), TWO(2, "楚"), THREE(3, "燕"), FOUR(4, "赵"), FIVE(5, "魏"), SIX(6, "韩");
  5. @Getter
  6. private Integer retcode;
  7. @Getter
  8. private String retMessage;
  9. CountryEnum(Integer retcode, String retMessage) {
  10. this.retcode = retcode;
  11. this.retMessage = retMessage;
  12. }
  13. public static CountryEnum forEach_CountryEnum(int index) {
  14. CountryEnum[] myArray = CountryEnum.values();
  15. for (CountryEnum element : myArray) {
  16. if (index == element.getRetcode()) {
  17. return element;
  18. }
  19. }
  20. return null;
  21. }
  22. }
  1. package s02.e07;
  2. import java.util.concurrent.CountDownLatch;
  3. public class CountDownLatchDemo {
  4. public static void main(String[] args) throws Exception {
  5. CountDownLatch countDownLatch = new CountDownLatch(6);
  6. for (int i = 1; i <= 6; i++) {
  7. new Thread(() -> {
  8. System.out.println(Thread.currentThread().getName() + "\t 国,被灭");
  9. countDownLatch.countDown();
  10. }, CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();
  11. }
  12. countDownLatch.await();
  13. System.out.println(Thread.currentThread().getName() + "\t*****************秦帝国,一统华夏");
  14. }
  15. }

image.png

7.2 CyclicBarrier

CyclicBarrier 的字面意思是可循环(Cyclic)使用的屏障(Barrier)。它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最后一个线程到达屏障时,屏障才会开门,所有被屏障拦截的线程才会继续干活,线程进入屏障通过 CyclicBarrier 的 await() 方法。

  1. package s02.e07;
  2. import java.util.concurrent.BrokenBarrierException;
  3. import java.util.concurrent.CyclicBarrier;
  4. public class cyclicBarrierDemo {
  5. public static void main(String[] args) {
  6. // CyclicBarrier( int parties,Runnable barrierAction)
  7. CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
  8. System.out.println("*******召唤神龙");
  9. });
  10. for (int i = 1; i <= 7; i++) {
  11. final int tempInt = i;
  12. new Thread(() -> {
  13. System.out.println(Thread.currentThread().getName() + "\t收集到第:" + tempInt + "龙珠");
  14. try {
  15. cyclicBarrier.await();
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. } catch (BrokenBarrierException e) {
  19. e.printStackTrace();
  20. }
  21. }, String.valueOf(i)).start();
  22. }
  23. }
  24. }

image.png

7.3 Semaphore

信号量主要用于两个目的,一个是用于多个共享资源的互斥使用,另一个用于并发线程数的控制。

  1. package s02.e07;
  2. import java.util.concurrent.Semaphore;
  3. import java.util.concurrent.TimeUnit;
  4. public class SemaphoreDemo {
  5. public static void main(String[] args) {
  6. Semaphore semaphore = new Semaphore(3); // 模拟3个停车位
  7. for (int i = 1; i <= 6; i++) { // 模拟6部汽车
  8. new Thread(() -> {
  9. try {
  10. semaphore.acquire();
  11. System.out.println(Thread.currentThread().getName() + "\t抢到车位"); // 门暂停一会儿线程
  12. try {
  13. TimeUnit.SECONDS.sleep(3);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. System.out.println(Thread.currentThread().getName() + "\t停车3秒后离开车位");
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. } finally {
  21. semaphore.release();
  22. }
  23. }, String.valueOf(i)).start();
  24. }
  25. }
  26. }

image.png
semaphore 使用完后要释放,以便下一个等候的线程进入