mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 16:14:58 +00:00
#271 implements producer-consumer
This commit is contained in:
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user