Java 11 migrate remaining p (#1122)
* Moves partial-response to Java 11 * Moves pipeline to Java 11 * Moves poison-pill to Java 11 * Moves priority-queue to Java 11 * Moves private-class-data to Java 11 * Moves producer-consumer to Java 11 * Moves promise to Java 11 * Moves property to Java 11 * Moves prototype to Java 11 * Moves proxy to Java 11 * Corrects checkstyle errors * Fixes build for pipeline pattern
This commit is contained in:
committed by
Ilkka Seppälä
parent
1401accb4f
commit
428efc7d53
@ -26,20 +26,18 @@ package com.iluwatar.producer.consumer;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Producer Consumer Design pattern is a classic concurrency or threading pattern which reduces
|
||||
* coupling between Producer and Consumer by separating Identification of work with
|
||||
* Execution of Work.
|
||||
* coupling between Producer and Consumer by separating Identification of work with Execution of
|
||||
* Work.
|
||||
*
|
||||
* <p>In producer consumer design pattern a shared queue is used to control the flow and this
|
||||
* separation allows you to code producer and consumer separately. It also addresses the issue
|
||||
* of different timing require to produce item or consuming item. by using producer consumer
|
||||
* pattern both Producer and Consumer Thread can work with different speed.
|
||||
*
|
||||
* separation allows you to code producer and consumer separately. It also addresses the issue of
|
||||
* different timing require to produce item or consuming item. by using producer consumer pattern
|
||||
* both Producer and Consumer Thread can work with different speed.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@ -47,18 +45,17 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args
|
||||
* command line args
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
ItemQueue queue = new ItemQueue();
|
||||
var queue = new ItemQueue();
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
var executorService = Executors.newFixedThreadPool(5);
|
||||
for (var i = 0; i < 2; i++) {
|
||||
|
||||
final Producer producer = new Producer("Producer_" + i, queue);
|
||||
final var producer = new Producer("Producer_" + i, queue);
|
||||
executorService.submit(() -> {
|
||||
while (true) {
|
||||
producer.produce();
|
||||
@ -66,8 +63,8 @@ public class App {
|
||||
});
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
final Consumer consumer = new Consumer("Consumer_" + i, queue);
|
||||
for (var i = 0; i < 3; i++) {
|
||||
final var consumer = new Consumer("Consumer_" + i, queue);
|
||||
executorService.submit(() -> {
|
||||
while (true) {
|
||||
consumer.consume();
|
||||
|
@ -46,10 +46,9 @@ public class Consumer {
|
||||
* Consume item from the queue.
|
||||
*/
|
||||
public void consume() throws InterruptedException {
|
||||
|
||||
Item item = queue.take();
|
||||
var item = queue.take();
|
||||
LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name,
|
||||
item.getId(), item.getProducer());
|
||||
item.getId(), item.getProducer());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import java.util.Random;
|
||||
* to queue.
|
||||
*/
|
||||
public class Producer {
|
||||
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
private final ItemQueue queue;
|
||||
@ -49,7 +49,7 @@ public class Producer {
|
||||
*/
|
||||
public void produce() throws InterruptedException {
|
||||
|
||||
Item item = new Item(name, itemId++);
|
||||
var item = new Item(name, itemId++);
|
||||
queue.put(item);
|
||||
Thread.sleep(RANDOM.nextInt(2000));
|
||||
}
|
||||
|
Reference in New Issue
Block a user