Semaphore学习一

一、定义
是JUC包下的一个工具类,我们可以通过其限制执行的线程数量,达到限流的效果。
当一个线程执行时先通过其方法进行获取许可操作,获取到许可的线程继续执行业务逻辑,当线程执行完成后进行释放许可操作,未获取达到许可的线程进行等待或者直接结束。
可以把它简单的理解成我们停车场入口立着的那个显示屏,每有一辆车进入停车场显示屏就会显示剩余车位减1,每有一辆车从停车场出去,显示屏上显示的剩余车辆就会加1,当显示屏上的剩余车位为0时,停车场入口的栏杆就不会再打开,车辆就无法进入停车场了,直到有一辆车从停车场出去为止。

二、方法
构造方法

    /*** Creates a {@code Semaphore} with the given number of* permits and nonfair fairness setting.** @param permits the initial number of permits available.*        This value may be negative, in which case releases*        must occur before any acquires will be granted.*/public Semaphore(int permits) {sync = new NonfairSync(permits);}

表示许可线程的数量

    /*** Creates a {@code Semaphore} with the given number of* permits and the given fairness setting.** @param permits the initial number of permits available.*        This value may be negative, in which case releases*        must occur before any acquires will be granted.* @param fair {@code true} if this semaphore will guarantee*        first-in first-out granting of permits under contention,*        else {@code false}*/public Semaphore(int permits, boolean fair) {sync = fair ? new FairSync(permits) : new NonfairSync(permits);}

fair表示公平性,如果设置为true,表示公平,那么等待最久的线程先执行

    /*** Acquires a permit from this semaphore, blocking until one is* available, or the thread is {@linkplain Thread#interrupt interrupted}.** <p>Acquires a permit, if one is available and returns immediately,* reducing the number of available permits by one.** <p>If no permit is available then the current thread becomes* disabled for thread scheduling purposes and lies dormant until* one of two things happens:* <ul>* <li>Some other thread invokes the {@link #release} method for this* semaphore and the current thread is next to be assigned a permit; or* <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread.* </ul>** <p>If the current thread:* <ul>* <li>has its interrupted status set on entry to this method; or* <li>is {@linkplain Thread#interrupt interrupted} while waiting* for a permit,* </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.** @throws InterruptedException if the current thread is interrupted*/public void acquire() throws InterruptedException {sync.acquireSharedInterruptibly(1);}

表示线程获取1个许可,那么线程许可数量相应减少一个,获取一个令牌,在获取到令牌、或者被其他线程调用中断之前线程一直处于阻塞状态。

    /*** Releases a permit, returning it to the semaphore.** <p>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.** <p>There is no requirement that a thread that releases a permit must* have acquired that permit by calling {@link #acquire}.* Correct usage of a semaphore is established by programming convention* in the application.*/public void release() {sync.releaseShared(1);}

表示释放一个许可,那么线程许可数量相应增加,释放一个令牌,唤醒一个获取令牌不成功的阻塞线程。

    /*** Releases the given number of permits, returning them to the semaphore.** <p>Releases the given number of permits, increasing the number of* available permits by that amount.* If any threads are trying to acquire permits, then one* is selected and given the permits that were just released.* If the number of available permits satisfies that thread's request* then that thread is (re)enabled for thread scheduling purposes;* otherwise the thread will wait until sufficient permits are available.* If there are still permits available* after this thread's request has been satisfied, then those permits* are assigned in turn to other threads trying to acquire permits.** <p>There is no requirement that a thread that releases a permit must* have acquired that permit by calling {@link Semaphore#acquire acquire}.* Correct usage of a semaphore is established by programming convention* in the application.** @param permits the number of permits to release* @throws IllegalArgumentException if {@code permits} is negative*/public void release(int permits) {if (permits < 0) throw new IllegalArgumentException();sync.releaseShared(permits);}

表示一个线程释放n个许可,这个数量有参数permits决定,获取一个令牌,在获取到令牌、或者被其他线程调用中断、或超时之前线程一直处于阻塞状态。

/*** Acquires the given number of permits from this semaphore,* blocking until all are available,* or the thread is {@linkplain Thread#interrupt interrupted}.** <p>Acquires the given number of permits, if they are available,* and returns immediately, reducing the number of available permits* by the given amount.** <p>If insufficient permits are available then the current thread becomes* disabled for thread scheduling purposes and lies dormant until* one of two things happens:* <ul>* <li>Some other thread invokes one of the {@link #release() release}* methods for this semaphore, the current thread is next to be assigned* permits and the number of available permits satisfies this request; or* <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread.* </ul>** <p>If the current thread:* <ul>* <li>has its interrupted status set on entry to this method; or* <li>is {@linkplain Thread#interrupt interrupted} while waiting* for a permit,* </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.* Any permits that were to be assigned to this thread are instead* assigned to other threads trying to acquire permits, as if* permits had been made available by a call to {@link #release()}.** @param permits the number of permits to acquire* @throws InterruptedException if the current thread is interrupted* @throws IllegalArgumentException if {@code permits} is negative*/public void acquire(int permits) throws InterruptedException {if (permits < 0) throw new IllegalArgumentException();sync.acquireSharedInterruptibly(permits);}

表示一个线程获取n个许可,这个数量有参数permits决定

/*** Returns the current number of permits available in this semaphore.** <p>This method is typically used for debugging and testing purposes.** @return the number of permits available in this semaphore*/public int availablePermits() {return sync.getPermits();}

返回当前信号量线程许可数量

 /*** Returns an estimate of the number of threads waiting to acquire.* The value is only an estimate because the number of threads may* change dynamically while this method traverses internal data* structures.  This method is designed for use in monitoring of the* system state, not for synchronization control.** @return the estimated number of threads waiting for this lock*/public final int getQueueLength() {return sync.getQueueLength();}

返回等待获取许可的线程数的预估值

/*** Acquires a permit from this semaphore, blocking until one is* available.** <p>Acquires a permit, if one is available and returns immediately,* reducing the number of available permits by one.** <p>If no permit is available then the current thread becomes* disabled for thread scheduling purposes and lies dormant until* some other thread invokes the {@link #release} method for this* semaphore and the current thread is next to be assigned a permit.** <p>If the current thread is {@linkplain Thread#interrupt interrupted}* while waiting for a permit then it will continue to wait, but the* time at which the thread is assigned a permit may change compared to* the time it would have received the permit had no interruption* occurred.  When the thread does return from this method its interrupt* status will be set.*/public void acquireUninterruptibly() {sync.acquireShared(1);}

获取一个令牌,在获取到令牌之前线程一直处于阻塞状态(忽略中断)

/*** Acquires a permit from this semaphore, only if one is available at the* time of invocation.** <p>Acquires a permit, if one is available and returns immediately,* with the value {@code true},* reducing the number of available permits by one.** <p>If no permit is available then this method will return* immediately with the value {@code false}.** <p>Even when this semaphore has been set to use a* fair ordering policy, a call to {@code tryAcquire()} <em>will</em>* immediately acquire a permit if one is available, whether or not* other threads are currently waiting.* This &quot;barging&quot; behavior can be useful in certain* circumstances, even though it breaks fairness. If you want to honor* the fairness setting, then use* {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) }* which is almost equivalent (it also detects interruption).** @return {@code true} if a permit was acquired and {@code false}*         otherwise*/public boolean tryAcquire() {return sync.nonfairTryAcquireShared(1) >= 0;}

尝试获得令牌,返回获取令牌成功或失败,不阻塞线程。

    /*** Acquires a permit from this semaphore, if one becomes available* within the given waiting time and the current thread has not* been {@linkplain Thread#interrupt interrupted}.** <p>Acquires a permit, if one is available and returns immediately,* with the value {@code true},* reducing the number of available permits by one.** <p>If no permit is available then the current thread becomes* disabled for thread scheduling purposes and lies dormant until* one of three things happens:* <ul>* <li>Some other thread invokes the {@link #release} method for this* semaphore and the current thread is next to be assigned a permit; or* <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread; or* <li>The specified waiting time elapses.* </ul>** <p>If a permit is acquired then the value {@code true} is returned.** <p>If the current thread:* <ul>* <li>has its interrupted status set on entry to this method; or* <li>is {@linkplain Thread#interrupt interrupted} while waiting* to acquire a permit,* </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.** <p>If the specified waiting time elapses then the value {@code false}* is returned.  If the time is less than or equal to zero, the method* will not wait at all.** @param timeout the maximum time to wait for a permit* @param unit the time unit of the {@code timeout} argument* @return {@code true} if a permit was acquired and {@code false}*         if the waiting time elapsed before a permit was acquired* @throws InterruptedException if the current thread is interrupted*/public boolean tryAcquire(long timeout, TimeUnit unit)throws InterruptedException {return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));}

尝试获得令牌,在超时时间内循环尝试获取,直到尝试获取成功或超时返回,不阻塞线程。

    /*** Queries whether any threads are waiting to acquire. Note that* because cancellations may occur at any time, a {@code true}* return does not guarantee that any other thread will ever* acquire.  This method is designed primarily for use in* monitoring of the system state.** @return {@code true} if there may be other threads waiting to*         acquire the lock*/public final boolean hasQueuedThreads() {return sync.hasQueuedThreads();}

等待队列里是否还存在等待线程。

/*** Acquires and returns all permits that are immediately available.** @return the number of permits acquired*/public int drainPermits() {return sync.drainPermits();}

清空令牌把可用令牌数置为0,返回清空令牌的数量。

三、使用Semaphore实现停车场指示牌功能
每个停车场入口都有一个提示牌,上面显示着停车场的剩余车位还有多少,当剩余车位为0时,不允许车辆进入停车场,直到停车场里面有车离开停车场,这时提示牌上会显示新的剩余车位数。
业务场景 :

1、停车场容纳总停车量20。

2、当一辆车进入停车场后,显示牌的剩余车位数响应的减1.

3、每有一辆车驶出停车场后,显示牌的剩余车位数响应的加1。

4、停车场剩余车位不足时,车辆只能在外面等待。

实现代码

package com.util;import lombok.extern.slf4j.Slf4j;import java.util.Random;
import java.util.concurrent.Semaphore;
import static java.lang.Thread.sleep;/*** @author : lssffy* @Description : 线程的信号量控制* @date : 2023/12/17 21:32*/
@Slf4j
public class SemaphoreTest {public static void main(String[] args) {//创建Semaphore对象数量20Semaphore sp = new Semaphore(20);//100个线程同时运行for (int i = 0; i < 8; i++) {new Thread(new Runnable() {@Overridepublic void run() {try{System.out.println("===" + Thread.currentThread().getName()+"来到停车场");if(sp.availablePermits() == 0){System.out.println("车位不足,请耐心等待");}sp.acquire();//获取令牌尝试进入停车场System.out.println(Thread.currentThread().getName() + "成功进入停车场");Thread.sleep(new Random().nextInt(10000));//模拟车辆在停车场停留的时间System.out.println(Thread.currentThread().getName() + "驶出停车场");sp.release();//释放令牌,腾出停车场车位}catch (InterruptedException e){e.printStackTrace();}}},i+"号车").start();}}
}

执行结果
在这里插入图片描述
Semaphore实现原理
Semaphore初始化

Semaphore sp = new Semaphore(20);

1、当调用new Semaphore(20)方式时,默认会创建一个非公平的锁的同步阻塞队列
2、把初始化令牌数量赋值给同步队列的state状态,state的值代表当前所剩余的令牌数量
获取令牌

sp.acquire();

1、当前线程会尝试去同步队列获取一个令牌,获取令牌的过程就是使用原子的操作去修改同步队列的state,获取一个令牌则修改为state=state-1
2、当计算出来的state<0,则代表令牌数量不足,此时会创建一个Node节点加入阻塞队列,挂起当前线程
3、当计算出来的state>=0,则表示获取令牌成功

    /*** Acquires in shared mode, aborting if interrupted.  Implemented* by first checking interrupt status, then invoking at least once* {@link #tryAcquireShared}, returning on success.  Otherwise the* thread is queued, possibly repeatedly blocking and unblocking,* invoking {@link #tryAcquireShared} until success or the thread* is interrupted.* @param arg the acquire argument.* This value is conveyed to {@link #tryAcquireShared} but is* otherwise uninterpreted and can represent anything* you like.* @throws InterruptedException if the current thread is interrupted*/public final void acquireSharedInterruptibly(int arg)throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();//尝试获取令牌,arg为获取令牌个数,当可用令牌数量减去当前令牌数量结果小于0,则创建一个节点加入阻塞队列,挂起当前线程if (tryAcquireShared(arg) < 0)doAcquireSharedInterruptibly(arg);}
    /*** 1、创建节点,加入阻塞队列* 2、重双向链表的head、tail节点关系,清空无效节点* 3、挂起当前节点线程* Acquires in shared interruptible mode.* @param arg the acquire argument*/private void doAcquireSharedInterruptibly(int arg)throws InterruptedException {//创建节点加入阻塞队列final Node node = addWaiter(Node.SHARED);boolean failed = true;try {for (;;) {//获得当前节点pre节点final Node p = node.predecessor();if (p == head) {//返回锁的stateint r = tryAcquireShared(arg);if (r >= 0) {setHeadAndPropagate(node, r);p.next = null; // help GCfailed = false;return;}}//重组双向链表,清空无效节点,挂起当前线程if (shouldParkAfterFailedAcquire(p, node) &&parkAndCheckInterrupt())throw new InterruptedException();}} finally {if (failed)cancelAcquire(node);}}

释放令牌

sp.release();

当调用sp.release();方法时
1、线程会尝试释放一个令牌,释放令牌的过程就是把同步队列的state修改为state=state+1的过程
2、释放令牌成功之后,同时唤醒同步队列的一个线程
3、被唤醒的节点会重新尝试去修改state=state-1的操作,如果state>=0则获取令牌成功,否则重新进入阻塞队列,挂起线程

    /*** Releases in shared mode.  Implemented by unblocking one or more* threads if {@link #tryReleaseShared} returns true.* 释放共享锁,同时会唤醒同步队列的一个线程* @param arg the release argument.  This value is conveyed to*        {@link #tryReleaseShared} but is otherwise uninterpreted*        and can represent anything you like.* @return the value returned from {@link #tryReleaseShared}*/public final boolean releaseShared(int arg) {//释放共享锁if (tryReleaseShared(arg)) {//唤醒所有共享节点线程doReleaseShared();return true;}return false;}
    /*** Release action for shared mode -- signals successor and ensures* propagation. (Note: For exclusive mode, release just amounts* to calling unparkSuccessor of head if it needs signal.)* 唤醒同步队列中的一个线程*/private void doReleaseShared() {/** Ensure that a release propagates, even if there are other* in-progress acquires/releases.  This proceeds in the usual* way of trying to unparkSuccessor of head if it needs* signal. But if it does not, status is set to PROPAGATE to* ensure that upon release, propagation continues.* Additionally, we must loop in case a new node is added* while we are doing this. Also, unlike other uses of* unparkSuccessor, we need to know if CAS to reset status* fails, if so rechecking.*/for (;;) {Node h = head;if (h != null && h != tail) {int ws = h.waitStatus;//是否需要唤醒后续节点if (ws == Node.SIGNAL) {//修改状态为初始0if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))continue;            // loop to recheck casesunparkSuccessor(h);//唤醒h.nex节点线程}else if (ws == 0 &&!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))continue;                // loop on failed CAS}if (h == head)                   // loop if head changedbreak;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/231602.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python 全栈体系【四阶】(七)

第四章 机器学习 六、多项式回归 1. 什么是多项式回归 线性回归适用于数据呈线性分布的回归问题。如果数据样本呈明显非线性分布&#xff0c;线性回归模型就不再适用&#xff08;下图左&#xff09;&#xff0c;而采用多项式回归可能更好&#xff08;下图右&#xff09;。例…

数据安全传输基础设施平台(二)

5安全传输平台总体设计 5.1 方案特点 规范化&#xff1a;严格遵循各种相关规范设计。独立性&#xff1a;系统各子系统间互相独立&#xff0c;在保持系统间接口的前提下&#xff0c;各系统间的升级互不干扰。最小耦合性&#xff1a;各子系统进行严格功能分解&#xff0c;每个子…

PCL点云处理之点云置平(拟合平面绕中心旋转到绝对水平)(二百二十七)

PCL点云处理之点云置平(绕中心旋转到绝对水平)(二百二十七) 一、什么是点云置平二、算法流程三、算法实现一、什么是点云置平 有时候,我们处理的点云平面并非位于水平面,而是位于某个任一三维平面上,而大多数算法又只能在水平面处理,或者水平面的点云处理是相对更简单…

P2P应用

目录 一.P2P的简介 二.P2P的工作方式 1.具有集中目录服务器的P2P工作方式 2.具有全分布式结构的P2P文件共享程序 一.P2P的简介 P2P(对等连接)&#xff0c;是指两台主机在通信时&#xff0c;并不区分哪一个是服务请求方和哪一个是服务提供方。只要两台主机都运行了对等连接…

人工智能_机器学习069_SVM支持向量机_网格搜索_交叉验证参数优化_GridSearchCV_找到最优的参数---人工智能工作笔记0109

然后我们再来说一下SVC支持向量机的参数优化,可以看到 这次我们需要,test_data这个是测试数据,容纳后 train_data这个是训练数据 这里首先我们,导出 import numpy as np 导入数学计算包 from sklearn.svm import SVC 导入支持向量机包 分类器包 def read_data(path): wit…

从事开发近20年,经历过各种技术的转变和进步

1、jsp、javabean、servlet、jdbc。 2、Struts1、hibernate、spring。 3、webwork、ibatis、spring 4、Struts2、mybatis、spring 5、spring mvc &#xff0c;spring全家桶 6、dubbo&#xff0c;disconf 微服务&#xff0c;soa 7、springboot 全家桶 8、docker 9、dock…

AXure的情景交互

目录 导语&#xff1a; 1.erp多样性登录界面 2.主页跳转 3.省级联动​编辑 4. 下拉加载 导语&#xff1a; Axure是一种流行的原型设计工具&#xff0c;可以用来创建网站和应用程序的交互原型。通过Axure&#xff0c;设计师可以创建情景交互&#xff0c;以展示用户与系统的交…

力扣题目学习笔记(OC + Swift) 14. 最长公共前缀

14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀&#xff0c;返回空字符串 “”。 方法一 竖向扫描法 个人感觉纵向扫描方式比较直观&#xff0c;符合人类理解方式&#xff0c;从前往后遍历所有字符串的每一列&#xff0c;比较相同列上的…

出国旅游需要注意些什么

出国旅游是一种令人兴奋、令人期待的经历。然而&#xff0c;在进行这种经历之前&#xff0c;有几件事情是需要注意的。本文将为您介绍出国旅游需要注意的一些重要事项。首先&#xff0c;为了确保您的出国旅行顺利进行&#xff0c;您应该提前办理好您的签证和护照。不同国家对于…

Idea远程debugger调试

当我们服务部署在服务器上&#xff0c;我们想要像在本地一样debug,就可以使用idea自带的Remote JVM Debug 创建Remote JVM Debug服务器启动jar打断点进入断点 当我们服务部署在服务器上&#xff0c;我们想要像在本地一样debug,就可以使用idea自带的 Remote JVM Debug) 创建Rem…

flask搞个简单登录界面

登录界面 直接放上login.html模板&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Lo…

JVM-11-运行时栈帧结构

“栈帧”&#xff08;Stack Frame&#xff09;则是用于支持虚拟机进行方法调用和方法执行背后的数据结构&#xff0c;它也是虚拟机运行时数据区中的虚拟机栈&#xff08;Virtual MachineStack&#xff09;的栈元素。 栈帧存储了方法的局部变量表、操作数栈、动态连接和方法返回…

docker在线安装nginx

1、查看所有镜像 1、不带容器卷常规启动nginx&#xff0c;命令如下 docker run --name nginx-test -p 8089:80 -d a6bd71f48f68 2、在宿主机创建/usr/local/data/nginxdocker/目录&#xff0c;在此目录下创建html和logs文件夹&#xff0c;然后将容器内的 nginx.conf 和 html 下…

01-从JDK源码级别彻底剖析JVM类加载机制

文章目录 类加载运行全过程类加载器和双亲委派机制类加载器初始化过程双亲委派机制为什么要设计双亲委派机制&#xff1f;全盘负责委托机制自定义类加载器 打破双亲委派机制Tomcat打破双亲委派机制Tomcat自定义加载器详解模拟实现Tomcat的JasperLoader热加载 补充&#xff1a;H…

AR室内导航如何实现?技术与原理分析

随着科技的进步&#xff0c;我们生活中许多方面正在被重新定义。其中之一就是导航&#xff0c;尤其是室内导航。增强现实&#xff08;AR&#xff09;技术的出现为室内导航带来了革命性的变革。本文将深入探讨AR室内导航的技术与原理&#xff0c;以及它如何改变我们的生活方式。…

数据结构(Chapter Two -02)—顺序表基本操作实现

在前一部分我们了解线性表和顺序表概念&#xff0c;如果有不清楚可以参考下面的博客&#xff1a; 数据结构(Chapter Two -01)—线性表及顺序表-CSDN博客 首先列出线性表的数据结构&#xff1a; #define MaxSize 50 //定义顺序表最大长度 typedef struct{ElemType data…

springboot解决XSS存储型漏洞

springboot解决XSS存储型漏洞 XSS攻击 XSS 攻击&#xff1a;跨站脚本攻击(Cross Site Scripting)&#xff0c;为不和 前端层叠样式表(Cascading Style Sheets)CSS 混淆&#xff0c;故将跨站脚本攻击缩写为 XSS。 XSS(跨站脚本攻击)&#xff1a;是指恶意攻击者往 Web 页面里插…

八.创建和管理表

目录 1. 基础知识1.1 一条数据存储的过程1.2 标识符命名规则1.3 MySQL中的数据类型 2. 创建和管理数据库2.2 使用数据库2.3 修改数据库 3. 创建表3.1 创建方式13.2 创建方式23.4 查看数据表结构 4. 修改表4.1 追加一个列4.2 修改一个列4.3 重命名一个列4.4 删除一个列 5. 重命名…

工作:三菱PLC程序开发流程总结

工作&#xff1a;三菱PLC程序开发流程总结 一、程序流程图 程序流程图是逻辑思维与动作流程的检查图&#xff0c;是保证逻辑思维合理的前提&#xff0c;写代码丢失方向可从程序流程图重新整理&#xff0c;程序流程图非常重要。 二、组态配置 组态配置是将所用到的基板和模块…

React基础巩固日志1

书写了一篇vue3的基础构建之后&#xff0c;不能带着各位一起学习vue3了&#xff0c;因为我要面试上海的前端岗位了&#xff0c;所以从现在开始&#xff0c;我要带着大家一起学习React了。 以下是我使用react书写的要掌握的react的知识点&#xff1a; ** ** 那么下面我们就一一通…