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.*;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class Fabrika {
- int max_capacity = 5;
- List<String> products = new ArrayList<>();
- static Lock lock = new ReentrantLock();
- public Fabrika(int capacity) {
- max_capacity = capacity;
- }
- public void produce(String s) throws InterruptedException{
- while(true) {
- lock.lock();
- if (products.size() < max_capacity) {
- System.out.println("Produced: " + s);
- products.add(s);
- lock.unlock();
- break;
- }
- lock.unlock();
- }
- }
- public void consume() throws InterruptedException{
- while(true) {
- lock.lock();
- if(products.size() > 0) {
- String s = products.remove(0);
- System.out.println("Consumed: " + s);
- lock.unlock();
- break;
- }
- lock.unlock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement