#271 implements producer-consumer

This commit is contained in:
hongsw committed 2015-10-28 09:55:05 +08:00
1 parent a2b8359ab5
commit 07faa2f625
11 files changed
+299

No files matched your search

@@ -0,0 +1,29 @@
package com.iluwatar.producer.consumer;
import java.util.Random;
/**
* Class responsible for producing unit of work that can be expressed as {@link Item} and submitted
* to queue
*/
public class Producer {
private final ItemQueue queue;
private final String name;
private int itemId = 0;
public Producer(String name, ItemQueue queue) {
this.name = name;
this.queue = queue;
}
public void produce() throws InterruptedException {
Item item = new Item(name, itemId++);
queue.put(item);
Random random = new Random();
Thread.sleep(random.nextInt(2000));
}
}