Advertisement
Josif_tepe

Untitled

Mar 26th, 2022
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class Fabrika {
  6.     int max_capacity = 5;
  7.     List<String> products = new ArrayList<>();
  8.     public Fabrika(int capacity) {
  9.         max_capacity = capacity;
  10.     }
  11.     public void produce(String s) throws InterruptedException{
  12.        while(true) {
  13.            synchronized (this) {
  14.                while(products.size() == max_capacity) {
  15.                    wait();
  16.                }
  17.                System.out.println("Produced: " + s);
  18.                products.add(s);
  19.                notify();
  20.                Thread.sleep(1000);
  21.            }
  22.        }
  23.     }
  24.     public void consume() throws InterruptedException{
  25.         while(true) {
  26.             synchronized (this) {
  27.                 while(products.size() == 0) {
  28.                     wait();
  29.                 }
  30.                 String s = products.remove(0);
  31.                 System.out.println("Consumed: " + s);
  32.                 notify();
  33.                 Thread.sleep(1000);
  34.             }
  35.         }
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement