其实Semaphore源码上的注释就说的比较清楚作用是如何的,下面是从源码翻译过来的解释:
计数信号灯。从概念上讲,信号量维护一组许可证。如果需要,acquire方法就是直到获得许可才会继续执行(阻塞)。release()方法会添加一个许可证,或者是释放acquire占据的许可。但是,其实没有实际存在许可证这个实体对象;simaphore只是记录可用的数量并相应地采取行动。
Semaphore通常用于限制线程的数量,而不能访问某些(物理或逻辑)资源。例如,以下是一个使用信号量控制对项目池的访问的类:
class Pool {
private static final int MAX_AVAILABLE = 100;
private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
public Object getItem() throws InterruptedException {
available.acquire();
return getNextAvailableItem();
}
public void putItem(Object x) {
if (markAsUnused(x))
available.release();
}
protected Object[] items = ... whatever kinds of items being managed
protected boolean[] used = new boolean[MAX_AVAILABLE];
protected synchronized Object getNextAvailableItem() {
for (int i = 0; i < MAX_AVAILABLE; ++i) {
if (!used[i]) {
used[i] = true;
return items[i];
}
}
return null; // not reached
}
protected synchronized boolean markAsUnused(Object item) {
for (int i = 0; i < MAX_AVAILABLE; ++i) {
if (item == items[i]) {
if (used[i]) {
used[i] = false;
return true;
} else
return false;
}
}
return false;
}
}
}
在获取item之前,每个线程必须从Semaphore获取许可,以确保item可供使用。当线程处理完item后,线程将返回到池中,并向Semaphore返回一个许可,从而允许另一个线程获取许可。调用acquire方法时不会保持同步锁定。信号量已经包装了限制访问池所需的同步,需要与维护池本身一致性所需的任何同步分开。
初始化为1的信号量,并且使用该信号量时,它最多只能有一个许可证可用,可以充当互斥锁。这通常被称为二进制信号量,因为它只有两种状态:一个许可证可用,或者零个许可证可用。以这种方式使用时,二进制信号量具有属性(与许多其他锁不同 类似java.util.concurrent.locks.Lock),锁可以由所有者以外的线程释放(因为信号量没有所有权的概念)。这在某些特定的上下文中非常有用,例如死锁恢复。
Semaphore支持配置fairness 是否公平性。当设置为false时,此类不保证线程获取许可的顺序,也就是允许抢占插队,调用acquire的线程可以在排队的线程之前分配到许可;设置为true时,Semaphore保证调用acquire方法的线程被选中,是按照它们调用这些方法的顺序(先进先出;FIFO)获得许可。注意:FIFO排序必须适用于这些方法中的特定内部执行点。因此,一个线程可以在另一个线程之前调用acquire,但在另一个线程之后到达排序点,并且在从方法返回时也是如此。tryAcquire方法不支持公平性设置,但接受任何可用的许可。
通常,用于控制资源访问的Semaphore应该被初始化为公平的,以确保没有线程被耗尽而无法访问资源(线程饥饿)。当将信号量用于其他类型的同步控制时,非公平排序的吞吐量优势往往超过公平性。
Semaphore还提供了可以一次可以处理多个许可证 acquire(int)和release(int)。当这些方法在没有公平性的情况下被使用时,要小心无限期推迟的风险。
OK 以上是源码解释的很清楚Semaphore的用途和用法。那么接下来看下Semaphore 是如何实现的呢?
首先看下Semaphore 构造器 共2个
/**
* 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);
}
这就看到主要有2个类,一个是FairSync 和 NonfairSync,那就是分为公平性和不公平性具体实现了。
/**
* NonFair version
*/
static final class NonfairSync extends Sync {
private static final long serialVersionUID = -2694183684443567898L;
NonfairSync(int permits) {
super(permits);
}
protected int tryAcquireShared(int acquires) {
return nonfairTryAcquireShared(acquires);
}
}
/**
* Fair version
*/
static final class FairSync extends Sync {
private static final long serialVersionUID = 2014338818796000944L;
FairSync(int permits) {
super(permits);
}
protected int tryAcquireShared(int acquires) {
for (;;) {
if (hasQueuedPredecessors())
return -1;
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
}
可以看到FairSync 和 NonfairSync 都继承于Sync, 并且重写了 tryAcquireShared 这个方法。
那先看下sync类的具体是什么
/**
* Synchronization implementation for semaphore. Uses AQS state
* to represent permits. Subclassed into fair and nonfair
* versions.
*/
abstract static class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 1192457210091910933L;
Sync(int permits) {
setState(permits);
}
final int getPermits() {
return getState();
}
final int nonfairTryAcquireShared(int acquires) {
for (;;) {
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
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");
if (compareAndSetState(current, next))
return true;
}
}
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;
}
}
final int drainPermits() {
for (;;) {
int current = getState();
if (current == 0 || compareAndSetState(current, 0))
return current;
}
}
}
可以看到Sync 是继承 AbstractQueuedSynchronizer 这个抽象类,而 AbstractQueuedSynchronizer 经常被称为 AQS,java.util.concurrent包中很多类都依赖于这个类,它是基于FIFO队列,可以用于构建锁或者其他相关同步装置。它利用了一个int来表示状态,期望它能够成为实现大部分同步需求的基础。使用的方法是继承,子类通过继承同步器并需要实现它的方法来管理其状态,管理的方式就是通过类似acquire和release的方式来操纵状态。然而多线程环境中对状态的操纵必须确保原子性,因此子类对于状态的把握,需要使用这个同步器提供的以下三个方法对状态进行操作:
java.util.concurrent.locks.AbstractQueuedSynchronizer.getState()
java.util.concurrent.locks.AbstractQueuedSynchronizer.setState(int)
java.util.concurrent.locks.AbstractQueuedSynchronizer.compareAndSetState(int, int)
具体AbstractQueuedSynchronizer 解析以后再单文章说,Semaphore 只要两个方法,上文也提到了就是acquire 和 release,那就来看看它们具体实现
//Semaphore
public void acquire() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
//AbstractQueuedSynchronizer
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
可以看到tryAcquireShared这个方法,其实在FairSync 和 NonfairSync都重写了。
首先看下FairSync
protected int tryAcquireShared(int acquires) {
for (;;) {
if (hasQueuedPredecessors())//查询是否有其他线程比比当前线程等待时间长--AbstractQueuedSynchronizer
return -1;
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
可以看到先判断是否有其他线程等待时间比自己长,如果有就返回-1;如果当前状态(信号量)少于目标信号量 或者 CAS修改成功返回剩余信号量,这也就保证按照顺序获取信号。
再看下NonfairSync
protected int tryAcquireShared(int acquires) {
return nonfairTryAcquireShared(acquires);
}
final int nonfairTryAcquireShared(int acquires) {
for (;;) {
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
可以看到就和FairSync少hasQueuedPredecessors()判断,其余逻辑是一样的。
那么当tryAcquireShared返回时大于等于0是就相当于获取到了信号量,也就是信号量其实用AQS的state的值控制。那么当小于0时,doAcquireSharedInterruptibly 又是什么逻辑呢?
/**
* 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 (;;) {
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);
}
}
这个方法是作用是以共享可中断模式获取。那么是怎么实现呢?
首先通过 addWaiter(Node.SHARED),添加当前线程到AQS的队列中。
然后再进行一次tryAcquireShared,如果成功了,就是返回,如果继续不成功,就走了
shouldParkAfterFailedAcquire和parkAndCheckInterrupt方法。
shouldParkAfterFailedAcquire方法就是 检查并更新失败的节点的状态获取。就是验证下当前线程是否正常,并纠正不正常的当前线程状态,并返回flase进行重新尝试获取信号。
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true;
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
parkAndCheckInterrupt 是方法进行park休眠 并且判断是否线程中断
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
接下来看release吧
public void release() {
sync.releaseShared(1);
}
sync的releaseShared就是AQS的releaseShared方法,并且进行重写。那继续看
/**
* 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;
}
看 tryReleaseShared 方法 是被Sync类重写了
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");
if (compareAndSetState(current, next))
return true;
}
}
逻辑比较简单,就是获取状态(信号量)并且进行CAS修改,返回结果true为止。
接下来就是doReleaseShared方法。
/**
* 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) {
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h);
}
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}
把源码翻译下:
共享模式的释放操作——向后继者发出信号并确保传播。(注意:对于独占模式,如果需要信号,释放相当于调用head的unparkSuccessor。)
确保一个发布可以传播,即使有其他正在进行的获取/发布。这是按照通常的方法进行的,如果头部需要信号,则尝试断开处理器的连接。但如果没有,则将status设置为PROPAGATE,以确保在发布时继续传播。此外,我们必须循环以防在执行此操作时添加新节点。另外,与unparkSuccessor的其他用法不同,我们需要知道CAS重置状态是否失败,如果是,则重新检查。
看不懂没关系,Semaphore的NODE是 SIGNAL,可以看上文的保存,所以关注点看unparkSuccessor方法就好。
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
* Thread to unpark is held in successor, which is normally
* just the next node. But if cancelled or apparently null,
* traverse backwards from tail to find the actual
* non-cancelled successor.
*/
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);
}
可以看到waitStatus其实在上面 doReleaseShared 已经改成0了,所以忽略。
下一个逻辑就是将next 休眠唤醒,unpark的线程被保存在后续节点中,后者通常只是下一个节点。但如果取消或明显为空,则从尾部向后遍历,以找到实际未取消的后继项。
好了,Semaphore主要逻辑就这些,其他的下次再说吧。
想了解更多知识,请关注我吧_