Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class Factory {
- List<String> vaccines = new ArrayList<>();
- int max_capacity = 5;
- Semaphore fabrika = new Semaphore(1);
- Semaphore proizveduvac = new Semaphore(10);
- Semaphore kupuvac = new Semaphore(10);
- public Factory(int capacity) {
- max_capacity = capacity;
- }
- public void produce_vaccine(String vaccine) throws InterruptedException{
- proizveduvac.acquire(); // namali go brojot na permits za proizveduvacot
- fabrika.acquire();
- while(vaccines.size() == max_capacity) {
- fabrika.release();
- Thread.sleep(50);
- fabrika.acquire();
- }
- vaccines.add(vaccine);
- System.out.println("proizvedna: " + vaccine);
- fabrika.release();
- kupuvac.release();
- }
- public void buy_vaccine() throws InterruptedException {
- kupuvac.acquire();
- fabrika.acquire();
- while(vaccines.size() == 0) {
- fabrika.release();
- Thread.sleep(50);
- fabrika.acquire();
- }
- System.out.println("kupena: " + vaccines.remove(0));
- proizveduvac.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement