diff --git a/5. labor/Producer/src/Application.java b/5. labor/Producer/src/Application.java index 9e702d5ee6ca5554e04dfde640e7dbb4b6671805..238560f8d02a89460e1840046b38619a05b33e92 100644 --- a/5. labor/Producer/src/Application.java +++ b/5. labor/Producer/src/Application.java @@ -2,12 +2,13 @@ public class Application { public static void main(String args[]) throws InterruptedException { Fifo storage = new Fifo(); - Producer prod = new Producer(storage, "producer", 250); - Consumer con = new Consumer(storage, "consumer", 500); + Thread threads[] = new Thread[7]; + for(int i=0;i<3;i++) + threads[i] = new Thread(new Producer(storage, "producer", (int) Math.random())); + for(int i=3;i<7;i++) + threads[i] = new Thread(new Consumer(storage, "consumer", (int) Math.random())); - Thread t1 = new Thread(prod); - Thread t2 = new Thread(con); - t1.start(); - t2.start(); + for(int i=0;i<7;i++) + threads[i].start(); } } \ No newline at end of file diff --git a/5. labor/Producer/src/Fifo.java b/5. labor/Producer/src/Fifo.java index d4d4bfc466d6eca2bd6c347c632411f21f24f82e..4dd62a4a2e68435cb5a6fa9c37dc5c6fc544cb48 100644 --- a/5. labor/Producer/src/Fifo.java +++ b/5. labor/Producer/src/Fifo.java @@ -7,23 +7,21 @@ public class Fifo{ } public synchronized void put(String e) throws InterruptedException { - if(s.size()>=10) - { + System.out.println("put: " + Thread.currentThread()); + while(s.size()>=10) this.wait(); - } - this.notifyAll(); + this.notify(); s.add(e); System.out.println("produced" + " " + e + " " + - System.currentTimeMillis()); + System.currentTimeMillis() % 10000); } synchronized String get() throws InterruptedException { - if(s.size()<=0) - { + System.out.println("get: " + Thread.currentThread()); + while(s.size()<=0) this.wait(); - } - this.notifyAll(); + this.notify(); String ret=s.get(0); s.remove(ret); return ret;