ITeye博客

A useful MultiSemaphore

willpower · 2007-03-07 11:50 · Study 学习 · Markdown

java 代码

  1. public class MultiSemaphore
  2. {
  3. protected int value;
  4. public MultiSemaphore(int v) {
  5. value = v;
  6. }
  7. //The P method here takes an integer parameter of how many instances of the
  8. //semaphore lock have been requested. The method will block until all
  9. //instances are available.
  10. public synchronized void P(int no) {
  11. while ((value - no) < 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. }

评论(1)

  1. #1 楼 · spainsherry · 2007-03-14 09:33

    革命尚未成功,同志仍需努力

评论功能维护中,当前仅展示历史评论。