Advertisement
safriansah

Contoh Metoda Wait() dan Notify()

Jul 24th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. public synchronized void enter(Object item){
  2.     while (count == BUFFER_SIZE){
  3.         try{
  4.             wait();
  5.         }
  6.         catch (InterruptedException e) {}
  7.     }
  8.     // add an item to the buffer
  9.     ++count;
  10.     buffer[in] = item;
  11.     in = (in+1) % BUFFER_SIZE;
  12.     notify();
  13. }
  14.  
  15. public synchronized void remove(Object item){
  16.     while (count == 0){
  17.         try{
  18.             wait();
  19.         }
  20.         catch (InterruptedException e) {}
  21.     }
  22.     // remove an item to the buffer
  23.     --count;
  24.     item = buffer[out];
  25.     out = (out+1) % BUFFER_SIZE;
  26.     notify();
  27.     return item;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement