首页 > 其他分享 >CountDownLatch、CycLicBarrier、Semaphore

CountDownLatch、CycLicBarrier、Semaphore

时间:2023-04-24 17:56:56浏览次数:45  
标签:thread Semaphore 信号量 线程 CountDownLatch println CycLicBarrier public permit

减计数器

 

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. 允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助程序 (一等多场景适用)
使用给定的计数初始化CountDownLatch。  由于countDown()方法的调用,直到当前计数达到零为止,await方法将阻塞。 A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations 之后,将释放所有等待线程,并立即返回所有随后的await调用 of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return 这是一种一次性的现象,计数器无法被重置。如果需要能够重置的版本,考虑使用CyclicBarrier; immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier. 来自 <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html>

案例

六国破灭后,秦国才一统华夏 枚举类
    1 public enum CountryEnum {
    2         /**
    3      * 国家
    4      *
    5      * @description 枚举常量 序数从0开始
    6      */
    7         ONE(1, "齐国"), TWO(2, "楚国"), THREE(3, "燕国"), FOUR(4, "韩国"), FIVE(5, "赵国"), SIX(6, "魏国");
    8 
    9         private Integer code;
   10         private String message;
   11 
   12         CountryEnum(int code, String message) {
   13                 this.code = code;
   14                 this.message = message;
   15         }
   16 
   17         public Integer getCode() {
   18                 return code;
   19         }
   20 
   21         public void setCode(Integer code) {
   22                 this.code = code;
   23         }
   24 
   25         public String getMessage() {
   26                 return message;
   27         }
   28 
   29         public void setMessage(String message) {
   30                 this.message = message;
   31         }
   32 
   33         /**
   34      * 匹配索引
   35      *
   36      * @param index
   37      * @return
   38      */
   39         public static CountryEnum forEach_CountryEnum(int index) {
   40                 //类型为 enum [ONE,TWO,THREE,FOUR,FIVE,SIX]
   41                 CountryEnum[] values = CountryEnum.values();
   42                 for (CountryEnum country : values
   43                 ) {
   44                         //System.out.println(country);
   45                         if (country.getCode() == index) {
   46                                 return country;
   47                         }
   48                 }
   49                 return null;
   50         }
   51 

demo

/**  * 减少计数锁存器用法实例  * @author 夜神  * @description 让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒  *                  CountDownLatch主要有两个方法,当一个或多个线程调用await方法      时,这些线程会阻塞。  *                  其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞),  *                  当计数器的值变为0时,因await方法阻塞的线程会被唤醒,继续执行。  * @like  解释: 六国破灭后秦才完成了一统。  *        main主线程必须要等前面6个线程完成全部工作后,自己才能开干  */
    1 public class CountDownLatchDemo {
    2         public static void main(String[] args) throws InterruptedException {
    3                 CountDownLatch count = new CountDownLatch(6);
    4                 for (int i = 1; i <= 6; i++) {
    5                         new Thread(()->{
    6                                 System.out.println(Thread.currentThread().getName()+"\t"+"被灭");
    7                                 //减少计数
    8                                 count.countDown();
    9                         },Objects.requireNonNull(CountryEnum.forEach_CountryEnum(i)).getMessage()).start();
   10                 }
   11                 //等待调用countDown()的线程执行完
   12                 count.await();
   13                 System.out.println("秦灭六国,一统华夏");
   14         }
   15 }

 

循环栅栏

CyclicBarrier循环栅栏,设置公共障碍点,会阻塞达到障碍点的线程,直到都达到后执行栅栏处的操作,然后放行线程,栅栏可以重复使用 适用于线程互相等待以执行公共操作的场景

 

 

 

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released. 允许一组线程互相等待以达到公共障碍点。CyclicBarriers在涉及固定线程大小的程序时很有用,必须偶尔等待对方。 这个栅栏被称为循环栅栏是因为它在释放等待的线程后可以被重用。
  A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties continue. 在最后的线程达到后,但是在任何线程被释放之前,CyclicBarrier支持的可选Runnable命令会在每个公共障碍点运行一次。 这个栅栏操作对任何一方继续之前更新共享状态是非常有用的。 来自 <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CyclicBarrier.html>     /**  * 循环栅栏  * @author 夜神  * @mean  *  CyclicBarrier  *  的字面意思是可循环(Cyclic)使用的屏障(Barrier)。它要做的事情是,  *  让一组线程到达一个屏障(也可以叫同步点)时被阻塞,  *  直到最后一个线程到达屏障时,执行一次BarrierAction操作,然后屏障才会开门,所有  *  被屏障拦截的线程才会继续干活。  *  线程进入屏障通过CyclicBarrier的await()方法。  *  * @description 集齐七颗龙珠后才召唤神龙  */

案例

集齐七颗龙珠,然后召唤神龙

    1 public class CyclicBarriersDemo {
    2         public static void main(String[] args) {
    3                 CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
    4                         System.out.println("集齐七颗龙珠可以召唤神龙了");
    5                 });
    6                 for (int i = 1; i <= 7 ; i++) {
    7                         new Thread(()->{
    8                                 try{
    9                                         System.out.println(Thread.currentThread().getName()+"\t星龙珠被收集");
   10                                         //公共障碍点
   11                                         cyclicBarrier.await();
   12                                         System.out.println("龙珠飞散了");
   13                                         TimeUnit.SECONDS.sleep(2);
   14                                         System.out.println(Thread.currentThread().getName()+"\t星龙珠又被收集了");
   15                                         //公共障碍点
   16                                         cyclicBarrier.await();
   17                                 }catch(Exception e){
   18                                         e.printStackTrace();
   19                                 }
   20                         },String.valueOf(i)).start();
   21                 }
   22         }
   23 

 信号量

适用于限制数量的场景 类似车位、门票

 

A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly. 一个计数信号量,从概念上讲,一个Semaphore维持着一组许可证。 如果必要,每个acquire()都会阻塞,直到获得许可,然后拿到它。每个release()添加一个许可证,可能释放一个阻塞的acquirer。但是,没有用到实际的许可对象,Semaphore仅仅保持可用量的计数并采取相应措施。
Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource 信号量通常用于限制线程数量,使其无法访问某些(物理或逻辑)资源
来自 <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Semaphore.html>  

 

Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted. Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one. If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

从这个信号量获取许可,阻塞直到有可用的信号量,或者线程被中断。

获取一个许可证(如果有)并立即返回,将可用许可证的数量减少一个。

如果没有可用的许可证,那么当前线程将被禁用线程调度目的,并处于休眠状态,直到发生以下两种情况之一:

  其他线程调用release()来释一个或多个放信号量   • Some other thread invokes the release() method for this semaphore and the current thread is next to be assigned a permit; or    其他线程被中断了   • Some other thread interrupts the current thread. If the current thread:   • has its interrupted status set on entry to this method; or   • is interrupted while waiting for a permit, then InterruptedException is thrown and the current thread's interrupted status is cleared.   Throws: InterruptedException - if the current thread is interrupted  

 

释放一个许可,并将其返回给信号量。

释放一个许可证,将可用许可证的数量增加一个。如果有线程试图获取许可证,则选择一个线程并给予刚刚释放的许可证。该线程被(重新)启

用以进行线程调度。

Releases a permit, returning it to the semaphore. Releases a permit, increasing the number of available permits by one. If any threads are trying to acquire a permit, then one is selected and given the permit that was just released. That thread is (re)enabled for thread scheduling purposes. 没有要求释放了许可证的线程必须通过调用acquire()获得许可证 信号量的正确使用是由应 用程序中的编程约定建立的。 There is no requirement that a thread that releases a permit must have acquired that permit by calling acquire(). Correct usage of a semaphore is established by programming convention in the application.
来自 <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Semaphore.html#release-->   /**  * @author 夜神  * @description 在(事先定义好)信号量上我们定义两种操作:  * acquire(获取) 当一个线程调用acquire操作时,它要么通过成功获取信号量(信号量减1),  * 要么一直等下去,直到有线程释放信号量,或超时。  * release(释放)实际上会将信号量的值加1,然后唤醒等待的线程。  *   * 信号量主要用于两个目的,一个是用于多个共享资源的互斥使用,另一个用于并发线程数的控制。  */  
    1 public class SemaphoreDemo {
    2         public static void main(String[] args) {
    3                 //三个停车位
    4                 Semaphore semaphore = new Semaphore(3);
    5                 //听六辆汽车
    6                 for (int i = 1; i <= 6; i++) {
    7                         new Thread(() -> {
    8                                 try {
    9                                         //查询可用车位数量
   10                                         System.out.println("驶入前可用车位"+semaphore.availablePermits());
   11                                         //获取车位
   12                                         semaphore.acquire();
   13                                         System.out.println(Thread.currentThread().getName() + "\t 号汽车驶入停车位");
   14                                         //停车时间
   15                                         TimeUnit.SECONDS.sleep(3);
   16                                         System.out.println(Thread.currentThread().getName() + "\t 号汽车驶出停车位");
   17                                 } catch (Exception e) {
   18                                         e.printStackTrace();
   19                                 } finally {
   20                                         //无论驶出车位还是爆炸了,释放停车位
   21                                         semaphore.release();
   22                                         //再次查询车位
   23                                         System.out.println("驶出后可用车位"+semaphore.availablePermits());
   24                                 }
   25                         }, String.valueOf(i)).start();
   26                 }
   27         }

 

 

 

                     

标签:thread,Semaphore,信号量,线程,CountDownLatch,println,CycLicBarrier,public,permit
From: https://www.cnblogs.com/deity-night/p/17350339.html

相关文章

  • CountDownLatch 学习笔记
    1.概念CountDownLatch是在JDK1.5的时候被引入的,位于java.util.concurrent并发包中,CountDownLatch叫做闭锁,也叫门闩。允许一个或多个线程一直等待,直到其他线程执行完成后再执行。2.工作原理CountDownLatch通过一个计数器来实现的。计数器的初始化值为线程的数量。每当一个线程......
  • AQS中的CyclicBarrier源码解析
    packagecn.com.pep;importjava.util.concurrent.BrokenBarrierException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;/**......
  • java CountDownLatch 实例
    一个线程等待CountDownLatch使用其await()等待其他线程完成(使用减值为0来判断是否完成)。是一个线程等待多个线程(1-N)的锁工具。以下为实例代码: packagecom.common;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.Executor;importjava.util.concurr......
  • semaphore简单实例
    semaphore是java里边的信号灯,感觉很像blockqueue,嗯 packagecom.mutiple;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Semaphore;publicclassTestSemaphore{ publicstaticvoidmain(String[]......
  • CountDownLatch、CyclicBarrier 使用区别
    CountDownLatch:所有子线程完成后,再执行主线程、CyclicBarrier:所有子线程就绪后,再执行子线程主要区别CountDownLatch:所有子线程完成后,再执行主线程CyclicBarrier:所有子线程就绪后,再执行子线程CountDownLatch所有子线程完成后,再执行主线程多线程ThreadP......
  • 【Java 并发】【九】【AQS】【七】Semaphore信号量底层机制原理
    1 前言接下来我们来看看Semaphore,也是基于之前讲解的AQS来实现的,建立在AQS体系之上的一个并发工具类。2  Semaphore是什么Semaphore,它是一个信号量,主要作用是用来控制并发中同一个时刻执行的线程数量,可以用来做限流器,或者流程控制器。在创建的时候会指定好它有多少个信号量......
  • Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。 用给定的计数初始化CountDownLatch。由于调用了countDown()方法,所以在当前计数到达零之前,await方法会一直受阻塞。之后,会释放所有等待的线程,awai......
  • CountDownLatch、CyclicBarrier 使用区别
    主要区别CountDownLatch:所有子线程完成后,再执行主线程CyclicBarrier:所有子线程就绪后,再执行子线程CountDownLatch所有子线程完成后,再执行主线程多线程ThreadPoolTaskExecutor应用SpringBoot下载文件CyclicBarrier有若干个线程,比如说有五个线程,需要它们都到达了某......
  • qt QSemaphore
    QSemaphore:一个通用的可计数的信号量,信号量实际上就是广义的互斥量。一个互斥量只能被锁定一次,然而一个信号量可以被获取多次。信号量常被用于保护一定数目的同类资源。......
  • CountDownLatch的实际中的使用
    接口太慢,其中一种方式就是使用多线程进行快速处理。介绍下,如何在项目中写代码的。 这里在for循环里使用调用,不是好方式,碍于接口只支持单个节点的查询。......