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));
|
||||
}
|
||||
|
@ -26,16 +26,13 @@ package com.iluwatar.producer.consumer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,13 @@
|
||||
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/27/15 - 11:01 PM
|
||||
*
|
||||
@ -41,15 +41,15 @@ public class ConsumerTest {
|
||||
|
||||
@Test
|
||||
public void testConsume() throws Exception {
|
||||
final ItemQueue queue = spy(new ItemQueue());
|
||||
for (int id = 0; id < ITEM_COUNT; id++) {
|
||||
final var queue = spy(new ItemQueue());
|
||||
for (var id = 0; id < ITEM_COUNT; id++) {
|
||||
queue.put(new Item("producer", id));
|
||||
}
|
||||
|
||||
reset(queue); // Don't count the preparation above as interactions with the queue
|
||||
final Consumer consumer = new Consumer("consumer", queue);
|
||||
final var consumer = new Consumer("consumer", queue);
|
||||
|
||||
for (int id = 0; id < ITEM_COUNT; id++) {
|
||||
for (var id = 0; id < ITEM_COUNT; id++) {
|
||||
consumer.consume();
|
||||
}
|
||||
|
||||
|
@ -23,8 +23,6 @@
|
||||
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
import static org.mockito.Matchers.any;
|
||||
@ -32,6 +30,8 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/28/15 - 12:12 AM
|
||||
*
|
||||
@ -42,8 +42,8 @@ public class ProducerTest {
|
||||
@Test
|
||||
public void testProduce() {
|
||||
assertTimeout(ofMillis(6000), () -> {
|
||||
final ItemQueue queue = mock(ItemQueue.class);
|
||||
final Producer producer = new Producer("producer", queue);
|
||||
final var queue = mock(ItemQueue.class);
|
||||
final var producer = new Producer("producer", queue);
|
||||
|
||||
producer.produce();
|
||||
verify(queue).put(any(Item.class));
|
||||
|
Reference in New Issue
Block a user