review comments

This commit is contained in:
priyank-sriv 2020-06-21 03:06:32 +05:30
parent 9ba617bbf6
commit b018d2c6cd
2 changed files with 35 additions and 32 deletions

View File

@ -25,7 +25,7 @@ public class CircularBuffer<E> {
int nextWriteSeq = writeSequence + 1;
data[nextWriteSeq % capacity] = element;
writeSequence += 1;
writeSequence++;
return true;
}
@ -37,7 +37,7 @@ public class CircularBuffer<E> {
if (isNotEmpty()) {
E nextValue = data[readSequence % capacity];
readSequence += 1;
readSequence++;
return nextValue;
}

View File

@ -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<String> buffer = new CircularBuffer<String>(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<String>(buffer, shapes));
Future<String[]> consumed = executorService.submit(new Consumer<String>(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<T> implements Runnable {
private String[] producerShapes;
private CircularBuffer<String> buffer;
private CircularBuffer<T> buffer;
private T[] items;
public Producer(String[] producerShapes, CircularBuffer<String> buffer) {
this.producerShapes = producerShapes;
public Producer(CircularBuffer<T> 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<T> implements Callable<T[]> {
private CircularBuffer<String> buffer;
private String[] consumedShapes;
private CircularBuffer<T> buffer;
private int expectedCount;
public Consumer(String[] consumedShapes, CircularBuffer<String> buffer) {
this.consumedShapes = consumedShapes;
public Consumer(CircularBuffer<T> 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;
}
}
}