You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: producer-consumer/README.md
+108Lines changed: 108 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,114 @@ Producer Consumer Design pattern is a classic concurrency pattern which reduces
11
11
coupling between Producer and Consumer by separating Identification of work with Execution of
12
12
Work.
13
13
14
+
## Explanation
15
+
16
+
Real-world example
17
+
18
+
> Consider a manufacturing process of item, the producer will need to pause the production when
19
+
> manufacturing pipeline is full and the consumer will need to pause the consumption of item
20
+
> when the manufacturing pipeline is empty. We can separate the process of production and consumption
21
+
> which work together and pause at separate times.
22
+
23
+
In plain words
24
+
25
+
> It provides a way to share data between multiple loops running at different rates.
26
+
27
+
Wikipedia says
28
+
> Dijkstra wrote about the case: "We consider two processes, which are called the 'producer'
29
+
> and the 'consumer' respectively. The producer is a cyclic process that produces a certain
30
+
> portion of information, that has to be processed by the consumer. The consumer is also a cyclic
31
+
> process that needs to process the next portion of information, as has been produced by the producer
32
+
> We assume the two processes to be connected for this purpose via a buffer with unbounded capacity."
33
+
34
+
**Programmatic Example**
35
+
36
+
Take our Producer and Consumer example from above. Consider we have a `Item` class that is produced by `Producer` class and is added to the `ItemQueue`.
Then, we have the `Consumer` class that takes the item from the item queue.
68
+
69
+
```java
70
+
71
+
@Slf4j
72
+
publicclassConsumer {
73
+
74
+
privatefinalItemQueue queue;
75
+
76
+
privatefinalString name;
77
+
78
+
publicConsumer(Stringname, ItemQueuequeue) {
79
+
this.name = name;
80
+
this.queue = queue;
81
+
}
82
+
83
+
/**
84
+
* Consume item from the queue.
85
+
*/
86
+
publicvoidconsume() throwsInterruptedException {
87
+
var item = queue.take();
88
+
LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name,
89
+
item.getId(), item.getProducer());
90
+
91
+
}
92
+
}
93
+
```
94
+
95
+
Now, during the manufacturing pipeline, we can instantiate objects from both the `Producer` and `Consumer` clasess as they produce and consumer items from the queue.
96
+
97
+
```java
98
+
var queue =newItemQueue();
99
+
var executorService =Executors.newFixedThreadPool(5);
100
+
for (var i =0; i <2; i++) {
101
+
102
+
finalvar producer =newProducer("Producer_"+ i, queue);
103
+
executorService.submit(() -> {
104
+
while (true) {
105
+
producer.produce();
106
+
}
107
+
});
108
+
}
109
+
110
+
for (var i =0; i <3; i++) {
111
+
finalvar consumer =newConsumer("Consumer_"+ i, queue);
0 commit comments