zoukankan      html  css  js  c++  java
  • 并发编程JUC系列AQS(CountDownLatch、CyclicBarrier、Semaphore)

    一、CountDownLatch

    package com.jonychen.test;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 并发编程java.util.concurrent(JUC)
     * AQS(AbstractQueuedSynchronizer)
     */
    public class JUCAQSDemo {
        public static void main(String[] args){
    /**
     * CountDownLatch用来控制一个线程等待多个线程,维护一个计数器cnt,
    * 每次调用countDown()会让计数器的值减一, 减到零时,
    * 那些因为调用await()方法而在等待的线程会被唤醒
    */
    final int totalThread=10; CountDownLatch countDownLatch =new CountDownLatch(totalThread); ExecutorService executorService =Executors.newCachedThreadPool(); for (int i = 0; i < totalThread; i++) { executorService.execute(()->{ System.out.println("jonychen run"); countDownLatch.countDown(); }); } try { countDownLatch.await(); System.out.println("end..."); executorService.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } } }

    运行截图:

    二、CyclicBarrier

     1 package com.jonychen.thread;
     2 
     3 import java.util.concurrent.BrokenBarrierException;
     4 import java.util.concurrent.CyclicBarrier;
     5 import java.util.concurrent.ExecutorService;
     6 import java.util.concurrent.Executors;
     7 
     8 /**
     9  * 并发编程java.util.concurrent(JUC)
    10  * AQS(AbstractQueuedSynchronizer)
    11  */
    12 public class JUCAQSCycliBarrier {
    13 /**
    14 *CyclicBarrier用来控制多个线程相互等待,只有当多个线程都到达时,这些线程才会继续执行,
    15 * 和CountDownLatch相似,都是通过维护计数器实现的,但他的计数器是递增的。每次执行await()
    16 * 方法后,计数器会加1,直到计数器的值和设置的值相同,等待的所有线程才会继续执行,和CountDownLatch
    17 * 的另一个区别是,CyclicBarrier的计数器可以循环使用,所以才叫他循环屏障
    18   */
    19     public static void main(String[] args){
    20         final int totalThread=10;
    21         CyclicBarrier cyclicBarrier =new CyclicBarrier(totalThread);
    22         ExecutorService executorService=Executors.newCachedThreadPool();
    23         for (int i = 0; i < totalThread; i++) {
    24             executorService.execute(()->{
    25                 System.out.println("before..*");
    26                 try {
    27                     cyclicBarrier.await();
    28                 } catch (InterruptedException e) {
    29                     e.printStackTrace();
    30                 } catch (BrokenBarrierException e) {
    31                     e.printStackTrace();
    32                 }
    33                 System.out.print("after   ");
    34             });
    35         }
    36         executorService.shutdown();
    37     }
    38 }

    运行截图:

    三、Semaphore

    package com.jonychen.thread;
    
    import sun.misc.Cleaner;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    
    /**
     * 并发编程java.util.concurrent(JUC)
     * AQS(AbstractQueuedSynchronizer)
     */
    public class JUCAQSSemaphore {
    
        public static void main(String[] args){
            /**
          *Semaphore就是操作系统中的信号量,可以控制对互斥资源的访问线程数
          *以下代码模拟了对某个服务的并发请求,每次只能有30个客户端同时访问,请求总数为 10。
          */
            final int clientCount=30;
            final int totalRequestCount=10;
            Semaphore semaphore =new Semaphore(clientCount);
            ExecutorService executorService=Executors.newCachedThreadPool();
            for (int i = 0; i < totalRequestCount; i++) {
                executorService.execute(()->{
                    try {
                        semaphore.acquire();
                        System.out.println(semaphore.availablePermits() + "");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        semaphore.release();
                    }
                });
            }
            executorService.shutdown();
        }
    }

    运行截图:

  • 相关阅读:
    Bootstrap历练实例:输入框组的大小
    bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀
    bootstrap历练实例:按钮作为输入框组前缀或后缀
    Bootstrap历练实例:垂直的按钮组
    [uiautomator篇][exist 存在,但click错误]
    [python篇][1]configparser 问题汇总
    [python篇][其他] python博客学习汇总
    [uiautomator篇][8] 增加应用读取内置存储卡的权限
    [uiautomator篇] 使用uiautomator需要导入uiautomator库
    [uiautomator篇][9]遇到问题
  • 原文地址:https://www.cnblogs.com/lxcy/p/9366564.html
Copyright © 2011-2022 走看看