Advertisement
ridjis

BoundedBuffer

Nov 9th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. /**
  2.  *
  3.  * Proizvođač i potrošač se vrte u beskonačnim petljama. U svakoj iteraciji oni pokušavaju da stave,
  4.  * odnosno preuzmu element iz bafera, nakon čega čekaju najviše 500 milisekundi pre prelaska na sledeću iteraciju.
  5.  * U realnim situacijama procedure produce() i consume() bi implementirale ponašanje koje odgovara stvarnim potrebama
  6.  * modeliranog problema. U našem primjeru, ovi metodi imaju samo simboličnu ulogu.
  7.  *
  8.  * Bafer je realizovan u potpunosti prateći definiciju ove strukture podataka.
  9.  * Kritične oblasti su ostavarene upotrebom modifikatora synchronized pri deklaraciji metoda put() i get().
  10.  * Na taj način  niti su onemogućene da istovremeno izvršavaju ove metode.
  11.  * Pored toga, pre nego što zaista pristupi dodavanju elementa u bafer, metod put() provjerava da li je bafer pun,
  12.  * i u slučaju da jeste blokira izvršavanje niti proizvođača. Nit proizvođača će ostati blokirana sve dok potrošač,
  13.  * u cilju preuzimanja jednog elementa iz bafera, ne izvrši metod get(), na čijem kraju se nalazi poziv metoda notify().
  14.  * Pozivom metoda notify(), nit proizvođača se budi i pristupa postavljanju novog elementa u bafer.
  15.  * Na isti način je ostvarena sinhronizacija i u suprotnom smijeru.
  16.  * Pre nego što zaista pristupi preuzimanju elemenata iz bafera, metod get provjerava da li je bafer prazan,
  17.  * i u slučaju da jeste, blokira izvršavanje niti potrošača. Nit potrošača će ostati blokirana sve dok proizvođač,
  18.  * u cilju dodavanja jednog elementa u bafer, ne izvrši metod put(), na čijem kraju se nalazi poziv metoda notify().
  19.  * Pozivom metoda notify(), nit potrošača se budi i pristupa preuzimanju postavljenog elementa iz bafera.
  20.  *
  21.  */
  22.  
  23. public class BoundedBuffer {
  24.     private volatile int size, head, tail;
  25.     private volatile boolean empty;
  26.     private volatile int[] array;
  27.    
  28.     public BoundedBuffer (int size) {
  29.         this.array = new int[size];
  30.         this.size = size;
  31.         this.head = 0;
  32.         this.tail = 0;
  33.         this.empty = true;
  34.     }
  35.    
  36.     public synchronized void put(int value) {
  37.         if (increment(tail) == head)
  38.             try {
  39.                 wait();
  40.             } catch (InterruptedException e) {}
  41.        
  42.         if (empty)
  43.             empty = false;
  44.         else
  45.             tail = increment(tail);
  46.        
  47.         array[tail] = value;
  48.        
  49.         System.out.println("Producted: " + value);
  50.         notify();
  51.     }
  52.    
  53.     public synchronized int get() {
  54.         if (empty)
  55.             try {
  56.                 wait();
  57.             } catch(InterruptedException e) {}
  58.        
  59.         int value = array[head];
  60.        
  61.         if (head == tail)
  62.             empty = true;
  63.         else
  64.             head = increment(head);
  65.        
  66.         System.out.println("Consumed: " + value);
  67.        
  68.         notify();
  69.         return value;
  70.     }
  71.    
  72.     private int increment(int value) {
  73.         return (value + 1) % size;
  74.     }
  75.    
  76.     public static void main(String[] args) {
  77.         BoundedBuffer buffer = new BoundedBuffer(3);
  78.         Producer producer = new Producer(buffer);
  79.         Consumer consumer = new Consumer(buffer);
  80.         producer.start();
  81.         consumer.start();
  82.     }
  83. }
  84.  
  85. class Producer extends Thread {
  86.     private BoundedBuffer buffer;
  87.    
  88.     public Producer (BoundedBuffer buffer) {
  89.         this.buffer = buffer;
  90.     }
  91.    
  92.     private int produce() {
  93.         return (int) (100 * Math.random());
  94.     }
  95.    
  96.     @Override
  97.     public void run() {
  98.         while (true) {
  99.             buffer.put(produce());
  100.             try {
  101.                 sleep((long) (500 * Math.random()));
  102.             } catch (InterruptedException e) {}
  103.         }
  104.     }
  105. }
  106.  
  107. class Consumer extends Thread {
  108.     private BoundedBuffer buffer;
  109.    
  110.     public Consumer (BoundedBuffer buffer) {
  111.         this.buffer = buffer;
  112.     }
  113.    
  114.     private void consume (int value) {}
  115.    
  116.     @Override
  117.     public void run() {
  118.         while (true) {
  119.             consume(buffer.get());
  120.             try {
  121.                 sleep((long) (500 * Math.random()));
  122.             } catch (InterruptedException e) {}
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement