Advertisement
keymasterviriya1150

thread

Sep 19th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1.  /*************************************************
  2. * Semaphore Class implemented using synchronized *
  3. * & the system calls wait() & notify()           *
  4. * ============================================== *
  5. * Name: Nantipat Tullwattana                     *
  6. * Major: B.Sc. of Software Engineering           *
  7. *************************************************/
  8. public class Semaphore
  9. {
  10.    
  11.     public Semaphore(int i){ value=i;}
  12.     /*Wait() implemented as synchronized to ensure that it is done atomically*/
  13.     public synchronized void Wait()
  14.     {
  15.         /*decremnt the semaphore*/
  16.         value--;
  17.         /*force the process to wait if value<0 */
  18.         if(value<0)
  19.             wait();
  20.     }
  21.     /*Signal() is implemented as synchronized to ensure it is done atomically*/
  22.     public synchronized void Signal()
  23.     {
  24.         value++; /* increment semaphore value*/
  25.         /*wake up any waiting processes*/
  26.         if(value<=0)
  27.             notify();
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement