From b018d2c6cd955d51fead1007494258a8421dcd98 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sun, 21 Jun 2020 03:06:32 +0530 Subject: [PATCH] review comments --- .../circularbuffer/CircularBuffer.java | 4 +- .../ProducerConsumerLiveTest.java | 63 ++++++++++--------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/data-structures/src/main/java/com/baeldung/circularbuffer/CircularBuffer.java b/data-structures/src/main/java/com/baeldung/circularbuffer/CircularBuffer.java index 271fc5f376..6b315265b4 100644 --- a/data-structures/src/main/java/com/baeldung/circularbuffer/CircularBuffer.java +++ b/data-structures/src/main/java/com/baeldung/circularbuffer/CircularBuffer.java @@ -25,7 +25,7 @@ public class CircularBuffer { int nextWriteSeq = writeSequence + 1; data[nextWriteSeq % capacity] = element; - writeSequence += 1; + writeSequence++; return true; } @@ -37,7 +37,7 @@ public class CircularBuffer { if (isNotEmpty()) { E nextValue = data[readSequence % capacity]; - readSequence += 1; + readSequence++; return nextValue; } diff --git a/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java b/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java index 06b1c5e4cf..83ce27043b 100644 --- a/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java +++ b/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java @@ -2,6 +2,11 @@ package com.baeldung.circularbuffer; import static org.junit.Assert.assertArrayEquals; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; import org.junit.jupiter.api.Test; @@ -11,38 +16,34 @@ public class ProducerConsumerLiveTest { private final String[] shapes = { "Circle", "Triangle", "Rectangle", "Square", "Rhombus", "Trapezoid", "Pentagon", "Pentagram", "Hexagon", "Hexagram" }; @Test - public void givenACircularBuffer_whenInterleavingProducerConsumer_thenElementsMatch() throws InterruptedException { + public void givenACircularBuffer_whenInterleavingProducerConsumer_thenElementsMatch() throws Exception { CircularBuffer buffer = new CircularBuffer(shapes.length); - String[] consumedShapes = new String[shapes.length]; - Thread producer = new Thread(new Producer(shapes, buffer)); - Thread consumer = new Thread(new Consumer(consumedShapes, buffer)); + ExecutorService executorService = Executors.newFixedThreadPool(2); - producer.start(); - consumer.start(); + executorService.submit(new Producer(buffer, shapes)); + Future consumed = executorService.submit(new Consumer(buffer, shapes.length)); - producer.join(); - consumer.join(); - - assertArrayEquals(shapes, consumedShapes); + String[] shapesConsumed = consumed.get(5L, TimeUnit.SECONDS); + assertArrayEquals(shapes, shapesConsumed); } - static class Producer implements Runnable { + static class Producer implements Runnable { - private String[] producerShapes; - private CircularBuffer buffer; + private CircularBuffer buffer; + private T[] items; - public Producer(String[] producerShapes, CircularBuffer buffer) { - this.producerShapes = producerShapes; + public Producer(CircularBuffer buffer, T[] items) { this.buffer = buffer; + this.items = items; } @Override public void run() { - for (int i = 0; i < producerShapes.length;) { - if (buffer.offer(producerShapes[i])) { - System.out.println("Produced: " + producerShapes[i]); + for (int i = 0; i < items.length;) { + if (buffer.offer(items[i])) { + System.out.println("Produced: " + items[i]); i++; LockSupport.parkNanos(5); } @@ -50,28 +51,30 @@ public class ProducerConsumerLiveTest { } } - static class Consumer implements Runnable { + @SuppressWarnings("unchecked") + static class Consumer implements Callable { - private CircularBuffer buffer; - private String[] consumedShapes; + private CircularBuffer buffer; + private int expectedCount; - public Consumer(String[] consumedShapes, CircularBuffer buffer) { - this.consumedShapes = consumedShapes; + public Consumer(CircularBuffer buffer, int expectedCount) { this.buffer = buffer; + this.expectedCount = expectedCount; } @Override - public void run() { - - for (int i = 0; i < consumedShapes.length;) { - String shape = buffer.poll(); - if (shape != null) { - consumedShapes[i++] = shape; + public T[] call() throws Exception { + T[] items = (T[]) new Object[expectedCount]; + for (int i = 0; i < items.length;) { + T item = buffer.poll(); + if (item != null) { + items[i++] = item; LockSupport.parkNanos(5); - System.out.println("Consumed: " + shape); + System.out.println("Consumed: " + item); } } + return items; } } }