php网站留言今天刚刚最新消息2023
Semaphore
作用
信号量,用于控制同时访问特定资源的线程数量,通过协调各个线程,以确保对共享资源的访问不会导致冲突或数据不一致等问题,有点类似令牌桶,内部维护一组许可证,
acquire获取许可证,获取到了就执行,没获取到就阻塞直到有许可证
release 释放许可证
示例代码
import java.util.concurrent.Semaphore;public class Test {public static void main(String[] args) {final Semaphore semaphore = new Semaphore(4);for (int i = 0; i < 5; i++) {new Thread(() -> {try {semaphore.acquire();// 获取许可证,没获取到就阻塞在这里System.out.println(Thread.currentThread().getName() + " 获得许可证,开始执行任务");Thread.sleep((long) (Math.random() * 1000));System.out.println(Thread.currentThread().getName() + " 执行任务完毕,释放许可证");semaphore.release();// 执行完毕释放许可证} catch (InterruptedException e) {e.printStackTrace();}}).start();}}
}
实现原理
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;}}}
内部通过继承AbstractQueuedSynchronizer
来实现其功能,有公平锁和非公平锁的实现,调用acquire方法,判断当前许可证数量大于0,就是用CAS操作许可证数量减1,线程就可以执行
许可证数量等于0 ,就把线程加入到AQS的等待队列中等待唤醒获取许可证再执行