//一个信号量,只有在池中还拥有许可时才允许线程继续执行。
//先看构造函数
//默认是非公平模式
public Semaphore(int permits) {
sync = new NonfairSync(permits);
}
NonfairSync(int permits) {
super(permits);
}
//设置状态
Sync(int permits) {
setState(permits);
}
public Semaphore(int permits, boolean fair) {
sync = fair ? new FairSync(permits) : new NonfairSync(permits);
}
//获取一个许可
public void acquire() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
//没有获得锁阻塞调用AQS里面的方法阻塞
doAcquireSharedInterruptibly(arg);
}
final int nonfairTryAcquireShared(int acquires) {
for (;;) {
int available = getState();
int remaining = available - acquires;
//如果remaining<0说明没有许可了。或者有许可直接CAS尝试设置state的值
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
private void doAcquireSharedInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
//获取指定数量的许可
public void acquire(int permits) throws InterruptedException {
if (permits < 0) throw new IllegalArgumentException();
sync.acquireSharedInterruptibly(permits);
}
//释放许可
public void release() {
sync.releaseShared(1);
}
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
protected final boolean tryReleaseShared(int releases) {
for (;;) {
int current = getState();
int next = current + releases;
if (next < current) // overflow
throw new Error("Maximum permit count exceeded");
//CAS设置当前许可数量
if (compareAndSetState(current, next))
return true;
}
}
//释放指定数量的许可
public void release(int permits) {
if (permits < 0) throw new IllegalArgumentException();
sync.releaseShared(permits);
}
//获取许可
public void acquireUninterruptibly() {
sync.acquireShared(1);
}
//不阻塞的直接获取许可,获取失败则直接返回false
public boolean tryAcquire() {
return sync.nonfairTryAcquireShared(1) >= 0;
}
//在一定时间内去尝试获取锁超时返回false
public boolean tryAcquire(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
//返回次信号量的可用许可数
public int availablePermits() {
return sync.getPermits();
}
//获取可用的所有许可并清空许可
public int drainPermits() {
return sync.drainPermits();
}
final int drainPermits() {
for (;;) {
int current = getState();
if (current == 0 || compareAndSetState(current, 0))
return current;
}
}
//缩减当前的可用许可量
protected void reducePermits(int reduction) {
if (reduction < 0) throw new IllegalArgumentException();
sync.reducePermits(reduction);
}
final void reducePermits(int reductions) {
for (;;) {
int current = getState();
int next = current - reductions;
if (next > current) // underflow
throw new Error("Permit count underflow");
if (compareAndSetState(current, next))
return;
}
}
//是否是公平模式
public boolean isFair() {
return sync instanceof FairSync;
}
//查看是否有线程等待获取
public final boolean hasQueuedThreads() {
return sync.hasQueuedThreads();
}
public final boolean hasQueuedThreads() {
return head != tail;
}
//获取正在等待许可的线程数量
public final int getQueueLength() {
return sync.getQueueLength();
}
public final int getQueueLength() {
int n = 0;
for (Node p = tail; p != null; p = p.prev) {
if (p.thread != null)
++n;
}
return n;
}
//将可能正在等待的线程封装到集合中
protected Collection<Thread> getQueuedThreads() {
return sync.getQueuedThreads();
}
public final Collection<Thread> getQueuedThreads() {
ArrayList<Thread> list = new ArrayList<Thread>();
for (Node p = tail; p != null; p = p.prev) {
Thread t = p.thread;
if (t != null)
list.add(t);
}
return list;
}
分享到:
相关推荐
Semaphore 源码解析 通过使用Semaphore可以在高并发场景进行限流控制处理
semaphore完整搭建方案,包括帮助文档、程序源码、可执行程序。按照帮助文档完整搭建可视化ansible部署解决方案
Semaphore进程间通讯测试源码,CreateSemaphoreW,OpenSemaphore,ReleaseSemaphore,WaitForSingleObjectEx
semaphore开发源码,ansible可视化部署服务程序,支持界面操作。
主要为大家详细介绍了Java并发系列之Semaphore源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
semaphore, if you just start to study c programing on linux ,maybe it will be one good example to help you understanding much knowledge ,like : pthread , file control ,how to run command in your own...
Semaphore的使用及其方法的使用。
Linux 下多线程编程-Pthread 与 Semaphore 的使用 Linux 下的多线程编程是通过使用 POSIX 线程接口,称为 pthread 实现的。要编写 Linux 下的多线程程序,需要使用头文件 pthread.h,连接时需要使用库 libpthread.a...
Semaphore T-BOX System OverviewPDF,Semaphore T-BOX System Overview
mutex和semaphore互斥程序实例
关于Android变成中的信号量semaphore,在使用过程中我们往往搞不清楚其信号量的申请和使用。这里主要针对信号量初始值为零的情况做介绍,其他情况不做讨论。初始值为零的情况较少使用,部分代码中的这个情况往往让...
Semaphore是计数信号量。Semaphore管理一系列许可证。每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。然而,其实并没有...
semaphore控制多线程循序执行,网上 找的例子更改的希望对大家有用
Semaphore_TBOX_产品-软件及系统功能
使用信号量(Semaphore)实现线程的同步
多线程(C++)同步Semaphore
信号量+共享存储区的进程间通信,互斥、同步
Ansible-semaphore.zip,可供选择的开源towerrelease,ansible是一个简单而强大的自动化引擎。它用于帮助配置管理、应用程序部署和任务自动化。
本文分析了内核同步及互斥的几种机制Waitqueue、Event及Semaphore的实现,详细分析了其实现流程。Event及Semaphore本质上都是基于Waitqueue和自旋锁实现的。总结了静态定义及动态初始化数据结构的相关规则,这对于...
C#语言Semaphore类用法实例,多线程 用于有多个同类型共享资源(比如端口)时,如何协调端口分配给线程访问资源的问题,是一个简单模拟实例(源代码)。