常用的辅助类(必会)

常用的辅助类(必会)

  • CountDownLatch

    • img
    • 代码实现
      public class Test04 {
          public static void main(String[] args) throws InterruptedException {
              // 总数是8,必须要执行任务的时候,再使用!
              CountDownLatch countDownLatch = new CountDownLatch(8);
              for (int i = 0; i < 8; i++) {
                  new Thread(()->{
                      System.out.println(Thread.currentThread().getName()+"out");
                      countDownLatch.countDown();  //意思为: -1
                  },String.valueOf(i)).start();
              }
              countDownLatch.await();  //等待计数器归零,才继续往下执行
              System.out.println("over");
          }
      }
      
    • 原理:
      countDownLatch.countDown(); // 数量-1

      countDownLatch.await(); // 等待计数器归零,然后再向下执行

      每次有线程调用 countDown() 数量-1,假设计数器变为0,countDownLatch.await() 就会被唤醒,继续 执行!

  • CyclicBarrier

    • 在这里插入图片描述
    • 代码实现:加法计数器

      public static void main(String[] args) throws InterruptedException {
          // 需要到达8个金币才能结束
          CyclicBarrier cyclicBarrier = new CyclicBarrier(8, () -> {
              System.out.println("8个金币集齐了!");
          });
      
          for (int i = 0; i < 8; i++) {
              final int count = i;   //Lambda操作i,需要一个变量
              new Thread(() -> {
                  System.out.println(Thread.currentThread().getName() + "集齐" + count + "个金币");
                  try {
                      cyclicBarrier.await();  //等待
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  } catch (BrokenBarrierException e) {
                      e.printStackTrace();
                  }
              }).start();
          }
      }
      
      //Thread-1集齐1个金币
      Thread-4集齐4个金币
      Thread-3集齐3个金币
      Thread-2集齐2个金币
      Thread-0集齐0个金币
      Thread-5集齐5个金币
      Thread-6集齐6个金币
      Thread-7集齐7个金币
      8个金币集齐了!
      
  • Semaphore

    • Semaphore:信号量

    • 在这里插入图片描述
    • 代码实现:抢车位!

      6辆车—-3个停车位

      public static void main(String[] args) throws InterruptedException {
          // 限流
          // 线程数量:3个停车位
          Semaphore semaphore = new Semaphore(3);
          for (int i = 0; i < 6; i++) {
              new Thread(() -> {
                  try {
                      //  acquire() 得到
                      semaphore.acquire();
                      System.out.println(Thread.currentThread().getName() + "抢到车位");
                      TimeUnit.SECONDS.sleep(3);  //停了3秒
                      System.out.println(Thread.currentThread().getName() + "离开车位");
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  } finally {
                      // release() 释放
                      semaphore.release();
                  }
              }, String.valueOf(i)).start();
          }
      }
      
    • 原理:

      • semaphore.acquire() 获得,假设如果已经满了,等待,等待被释放为止!
      • semaphore.release(); 释放,会将当前的信号量释放 + 1,然后唤醒等待的线程!

      作用: 多个共享资源互斥的使用!并发限流,控制大的线程数!

posted @ 2021-05-03 22:06  saxon宋  阅读(79)  评论(0)    收藏  举报