java 代码
- public class MultiSemaphore
- {
- protected int value;
- public MultiSemaphore(int v) {
- value = v;
- }
- //The P method here takes an integer parameter of how many instances of the
- //semaphore lock have been requested. The method will block until all
- //instances are available.
- public synchronized void P(int no) {
- while ((value - no) < 0) {
- try {
- wait();
- }
- catch (InterruptedException e) { }
- }
- value=value-no;
- }
- // the V method allows the caller to release a number of instances of the
- // semaphore lock
- public synchronized void V(int no ) {
- value=value+no;
- notify();
- }
- }