---
title: "A useful MultiSemaphore"
author: "willpower"
date: "2007-03-07T11:50:36Z"
tags: []
slug: "a-useful-multisemaphore"
draft: false
lastmod: "2007-03-07T11:50:36Z"
id: 57957
url: "https://willpower.iteye.com/blog/57957"
---

java 代码

01. public class MultiSemaphore
02. {
03. protected int value;
04. public MultiSemaphore(int v) {
05. value = v;
06. }
07. //The P method here takes an integer parameter of how many instances of the
08. //semaphore lock have been requested. The method will block until all
09. //instances are available.
10. public synchronized void P(int no) {
11. while ((value - no) &lt; 0) {
12. try {
13. wait();
14. }
15. catch (InterruptedException e) { }
16. }
17. value=value-no;
18. }
19. // the V method allows the caller to release a number of instances of the
20. // semaphore lock
21. public synchronized void V(int no ) {
22. value=value+no;
23. notify();
24. }
25. }