Advertisement
Josif_tepe

Untitled

Apr 2nd, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.Semaphore;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. public class Factory {
  8.     List<String> vaccines = new ArrayList<>();
  9.     int max_capacity = 5;
  10.     Semaphore fabrika = new Semaphore(1);
  11.     Semaphore proizveduvac = new Semaphore(10);
  12.     Semaphore kupuvac = new Semaphore(10);
  13.     public Factory(int capacity) {
  14.         max_capacity = capacity;
  15.     }
  16.     public void produce_vaccine(String vaccine) throws InterruptedException{
  17.         proizveduvac.acquire(); // namali go brojot na permits za proizveduvacot
  18.         fabrika.acquire();
  19.         while(vaccines.size() == max_capacity) {
  20.             fabrika.release();
  21.             Thread.sleep(50);
  22.             fabrika.acquire();
  23.         }
  24.         vaccines.add(vaccine);
  25.         System.out.println("proizvedna: " + vaccine);
  26.         fabrika.release();
  27.         kupuvac.release();
  28.     }
  29.     public void buy_vaccine() throws InterruptedException {
  30.         kupuvac.acquire();
  31.         fabrika.acquire();
  32.         while(vaccines.size() == 0) {
  33.             fabrika.release();
  34.             Thread.sleep(50);
  35.             fabrika.acquire();
  36.         }
  37.         System.out.println("kupena: " + vaccines.remove(0));
  38.         proizveduvac.release();
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement