一 Semaphore入门
1.1 什么是Semaphore
Semaphore,俗称信号量,它是操作系统中PV操作的原语在java的实现,它也是基于AbstractQueuedSynchronizer实现的。
Semaphore的功能非常强大,大小为1的信号量就类似于互斥锁,通过同时只能有一个线程获取信号量实现。大小为n(n>0)的信号量可以实现限流的功能,它可以实现只能有n个线程同时获取信号量。
什么是pv操作?
PV操作是操作系统一种实现进程互斥与同步的有效方法。PV操作与信号量(S)的处理相关,P表示通过的意思,V表示释放的意思。用PV操作来管理共享资源时,首先要确保PV操作自身执行的正确性。
P操作的主要动作是:
①S减1;
②若S减1后仍大于或等于0,则进程继续执行;
③若S减1后小于0,则该进程被阻塞后放入等待该信号量的等待队列中,然后转进程调度。
V操作的主要动作是:
①S加1;
②若相加后结果大于0,则进程继续执行;
③若相加后结果小于或等于0,则从该信号的等待队列中释放一个等待进程,然后再返回原进程继续执行或转进程调度。
1.2 Semaphore的常用方法
构造器
public Semaphore(int permits) {
sync = new NonfairSync(permits);
}
public Semaphore(int permits, boolean fair) {
sync = fair ? new FairSync(permits) : new NonfairSync(permits);
}
● permits 表示许可证的数量(资源数)
● fair 表示公平性,如果这个设为 true 的话,下次执行的线程会是等待最久的线程
常用方法
public void acquire() throws InterruptedException
public boolean tryAcquire()
public void release()
public int availablePermits()
public final int getQueueLength()
public final boolean hasQueuedThreads()
protected void reducePermits(int reduction)
● acquire() 表示尝试获取许可(获取不到则阻塞)。
● tryAcquire() 方法在没有许可的情况下会立即返回 false,要获取许可的线程不会阻塞。
● release() 表示释放许可。
● int availablePermits():返回此信号量中当前可用的许可证数。
● int getQueueLength():返回正在等待获取许可证的线程数。
● boolean hasQueuedThreads():是否有线程正在等待获取许可证。
● void reducePermit(int reduction):减少 reduction 个许可证
● Collection getQueuedThreads():返回所有等待获取许可证的线程集合
1.3 应用场景
可以用于做流量控制,特别是公用资源有限的应用场景
代码演示:
模拟一个每5S只能处理5个请求的限流Demo
/**
-
限流测试
-
@author wcy
*/
@Slf4j
public class SemaphoneDemo1 {/**
- 实现一个同时只能处理5个请求的限流器
*/
private static Semaphore semaphore = new Semaphore(5);
/**
- 定义一个线程池
*/
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(
10, 50,
60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(200));
/**
- 模拟执行方法
*/
public static void exec() {
try {
semaphore.acquire(1);
// 模拟真实方法执行
log.info(“执行exec方法”);
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
semaphore.release(1);
}
}
public static void main(String[] args) throws InterruptedException {
{for (; ; ) {Thread.sleep(100);// 模拟请求以10个/s的速度executor.execute(() -> exec());}}
}
}
运行结果: - 实现一个同时只能处理5个请求的限流器
可以看出,每个周期内只能5个线程执行了方法
二 Semaphore原理
学习Semaphore源码的时候我们有两个关注点:
-
Semaphore的加锁解锁(共享锁)逻辑实现
-
线程竞争锁失败入队阻塞逻辑和获取锁的线程释放锁唤醒阻塞线程竞争锁的逻辑实现
加锁逻辑(acquire)
public void acquire(int permits) throws InterruptedException {
if (permits < 0) throw new IllegalArgumentException();
sync.acquireSharedInterruptibly(permits);
}
这里调用了同步器的acquireSharedInterruptibly(int arg)方法
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
先看判断逻辑tryAcquireShared(arg)方法,这是同步器子类实现的
protected int tryAcquireShared(int acquires) {
return nonfairTryAcquireShared(acquires);
}
再看nonfairTryAcquireShared(acquires)方法
final int nonfairTryAcquireShared(int acquires) {
for (;😉 {
//获取许可证数量
int available = getState();
//减去当前线程使用的许可数
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
返回可用的许可数,如果<0,说明没有可用的许可,就会进入 doAcquireSharedInterruptibly(arg)方法,这个方法也是同步器实现的
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);
}
}
注意:Node node = addWaiter(Node.SHARED);这是构建队列的方法,但是和ReentrantLock不同的是,这里参数传的是Node.SHARED,第一个if逻辑是当同步队列中第一个线程被唤醒后,会进入这里重新竞争锁,竞争成功后,做出队的操作,我们假设这里是第一次构建队列,先看addWaiter(Node.SHARED)方法
static final Node SHARED = new Node();
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
这里第一个线程入队,会调用enq方法构建队列,后来的线程会进入if分支,加入队列尾部。当前线程Node的nextWaiter=Node.SHARED
private Node enq(final Node node) {
for (;😉 {
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
tail仍然为空,通过cas操作,新建一个头节点,这就是并发的精髓了,通过一个死循环,第二次循环的时候tail不为空,进入else逻辑,把当前线程所在的节点的前驱节点指向前边的结点,并把当前线程节点设置为尾结点。(这里通过cas保证线程安全问题),至此,我们的队列构建完成,回到doAcquireSharedInterruptibly方法中,可以看出,如果当前线程节点的前驱节点如果是头节点是话还会进行一次cas操作去尝试获取许可,假设还没有线程释放许可,返回负数,进入第二个if逻辑中,有两个判断方法,shouldParkAfterFailedAcquire(p, node)和parkAndCheckInterrupt()方法
先看shouldParkAfterFailedAcquire(p, node)方法
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;
}
先判断前驱节点的waitStatus是否为-1.如果不是-1,通过cas操作改为-1,返回false,外边是一个死循环,会第二次进入这个方法,这次判断为-1,返回true,进入parkAndCheckInterrupt()方法,
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
在这里,我们的线程就阻塞着了。
解锁逻辑(release):
public void release(int permits) {
if (permits < 0) throw new IllegalArgumentException();
sync.releaseShared(permits);
}
接着看releaseShared方法,这个是由同步器来实现的
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
接着看tryReleaseShared,这个是同步器子类实现的,主要目的就是释放一个资源许可。
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;
}
}
这里释放锁后,许可加1,执行doReleaseShared()方法
doReleaseShared()方法,
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;
}
}
把头节点的waitStatus置为0,调用unparkSuccessor方法
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);
}
拿到头结点的下一个节点,唤醒同步队列中阻塞的第一个线程,此时又会回到阻塞的地方doAcquireSharedInterruptibly方法中
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);
}
}
这时候,被阻塞的第一个线程被唤醒,重新进入循环,会进入第一个if中,此时调用tryAcquireShared方法可以拿到一个许可,也就是r>=0,
然后调用setHeadAndPropagate方法,这就是共享锁和独占锁的区别之一
setHeadAndPropagate
private void setHeadAndPropagate(Node node, int propagate) {
Node h = head; // Record old head for check below
//设置新的头节点
setHead(node);
if (propagate > 0 || h == null || h.waitStatus < 0 ||
(h = head) == null || h.waitStatus < 0) {
Node s = node.next;
if (s == null || s.isShared())
//唤醒下一个节点
doReleaseShared();
}
}
在这里先把当前线程的节点设置为新的头节点,再次尝试唤醒下一个节点,这样有个好处,就是资源释放得快的话,线程就持续被唤醒,这也就保证了Semaphone可以限流的原因,同时刻,只要有线程释放资源,其他线程就可以拿到许可进而执行自己的业务。