binarysemaphore.java
来自「a simple java mutlti thread concurrency 」· Java 代码 · 共 24 行
JAVA
24 行
package Synchronization;// is final so subclassing cannot upset if (...) wait() and notify()// but notify() (and wait() too) are public methods so still risky;// better to create a private object and do notify(), wait() inside itpublic final class BinarySemaphore extends Semaphore { public BinarySemaphore() {super();} // constructors// public BinarySemaphore(int initial) {super((initial!=0) ? 1:0);} public BinarySemaphore(int initial) { super(initial); // Don't be silent about bad initial value; tell the user! if (initial > 1) throw new IllegalArgumentException("initial>1"); } public BinarySemaphore(boolean initial) {super(initial ? 1:0);} public final synchronized void V() { super.V(); if (value > 1) value = 1; // cap the value }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?