| import java.util.concurrent.Semaphore; |
| |
| public class SemaphoreDemo { |
| |
| public static void main(String[] args) { |
| |
| Semaphore semaphore = new Semaphore(8); |
| |
| |
| for (int i = 0; i < 10; i++) { |
| new Thread(()->{ |
| try { |
| semaphore.acquire(); |
| System.out.println(Thread.currentThread().getName() + "开始执行"); |
| Thread.sleep(5000L); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } finally { |
| semaphore.release(); |
| } |
| }).start(); |
| } |
| } |
| |
| } |
| |
| # 控制台打印结果:最后线程8和线程9要过一会儿才打印,这是因为这2个线程被限制了,要等其他线程释放信号,才开始执行 |
| Thread-0开始执行 |
| Thread-6开始执行 |
| Thread-2开始执行 |
| Thread-7开始执行 |
| Thread-5开始执行 |
| Thread-4开始执行 |
| Thread-1开始执行 |
| Thread-3开始执行 |
| Thread-9开始执行 |
| Thread-8开始执行 |
| |
| Process finished with exit code 0 |
| # 查看Semaphore类 |
| public Semaphore(int permits) { |
| sync = new NonfairSync(permits); |
| } |
| |
| # 查看NonfairSync方法 |
| 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); |
| } |
| } |
| |
| # 查看super方法 |
| 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; |
| } |
| } |
| |
| # 查看acquire方法 |
| public void acquire() throws InterruptedException { |
| sync.acquireSharedInterruptibly(1); |
| } |
| |
| # 查看acquireSharedInterruptibly方法 |
| public final void acquireSharedInterruptibly(int arg) |
| throws InterruptedException { |
| if (Thread.interrupted()) |
| throw new InterruptedException(); |
| if (tryAcquireShared(arg) < 0) |
| doAcquireSharedInterruptibly(arg); |
| } |
| |
| # 查看tryAcquireShared方法 |
| protected int tryAcquireShared(int arg) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| # 查看实现类 |

| 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) |
| 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) |
| 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; |
| } |
| } |
| } |
| |
| # 查看release方法 |
| public void release() { |
| sync.releaseShared(1); |
| } |
| |
| # 查看releaseShared |
| public final boolean releaseShared(int arg) { |
| if (tryReleaseShared(arg)) { |
| doReleaseShared(); |
| return true; |
| } |
| return false; |
| } |
| |
| # 查看tryReleaseShared方法 |
| protected boolean tryReleaseShared(int arg) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| # 查看实现 |

| protected final boolean tryReleaseShared(int releases) { |
| for (;;) { |
| int current = getState(); |
| int next = current + releases; |
| if (next < current) |
| throw new Error("Maximum permit count exceeded"); |
| if (compareAndSetState(current, next)) |
| return true; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-05-16 真实机中安装CentOS7(一)