Advertisement
Josif_tepe

Untitled

Apr 2nd, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Factory {
  5.     List<String> vaccines = new ArrayList<>();
  6.     int max_capacity = 2;
  7.  
  8.     public Factory(int capacity) {
  9.         max_capacity = capacity;
  10.     }
  11.     public void produce_vaccine(String vaccine) throws InterruptedException{
  12.        while(true) {
  13.             synchronized (this) {
  14.                 while(vaccines.size() == max_capacity) {
  15.                     wait();
  16.                 }
  17.                 System.out.println("Sme proizvele vakcina: " + vaccine);
  18.                 vaccines.add(vaccine);
  19.                 notify();
  20.                 Thread.sleep(1000); // davame vreme da napravi sinhronizacija
  21.             }
  22.        }
  23.  
  24.     }
  25.     public void buy_vaccine() throws InterruptedException {
  26.        while(true) {
  27.            synchronized (this) {
  28.                while(vaccines.size() == 0) {
  29.                    wait();
  30.                }
  31.                String vaccine = vaccines.remove(0);
  32.                System.out.println("Sme ja kupile: " + vaccine);
  33.                notify();
  34.                Thread.sleep(1000);
  35.            }
  36.        }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement