1 36 package org.columba.ristretto.concurrency; 37 38 43 public class Semaphore { 44 private int value; 45 46 51 public Semaphore(int value) { 52 this.value = value; 53 } 54 55 58 public Semaphore() { 59 this(0); 60 } 61 62 68 public synchronized void acquire(int n) { 69 while( value < n) { 70 try { 71 wait(); 72 } catch (InterruptedException e) { 73 e.printStackTrace(); 74 } 75 } 76 77 value -= n; 78 } 79 80 85 public void acquire() { 86 acquire(1); 87 } 88 89 94 public synchronized void release(int n) { 95 value += n; 96 97 notify(); 98 } 99 } 100 | Popular Tags |