Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moves producer-consumer to Java 11
  • Loading branch information
anuragagarwal561994 committed Dec 29, 2019
commit 80c0a0dfc262804ad1b3e0fc7f54f7a60fd92758
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,45 @@
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 {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* 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();
}
});
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public Consumer(String name, ItemQueue queue) {
* 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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* to queue.
*/
public class Producer {

private static final Random RANDOM = new Random();

private final ItemQueue queue;
Expand All @@ -49,7 +49,7 @@ public Producer(String name, ItemQueue queue) {
*/
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@
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[]{});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

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;
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
*
Expand All @@ -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));
Expand Down