mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 23:19:56 +00:00
Uses concurrent queue.
This commit is contained in:
parent
7dbf8cb992
commit
a0ce9a6817
@ -24,10 +24,8 @@ public class Consumer implements Runnable {
|
||||
throw new RuntimeException("Error acquiring semaphore " + e);
|
||||
}
|
||||
|
||||
if (store.size() > 0) {
|
||||
store.remove();
|
||||
System.out.println("Consumed: " + name + " Left units :" + store.size());
|
||||
}
|
||||
|
||||
forProducer.release();
|
||||
|
||||
|
@ -25,10 +25,8 @@ public class Producer implements Runnable {
|
||||
throw new RuntimeException("Error acquiring semaphore " + e);
|
||||
}
|
||||
|
||||
if (store.size() < maxSize) {
|
||||
store.add(new UnitOfWork());
|
||||
System.out.println("Produced: " + name + " Left units :" + store.size());
|
||||
}
|
||||
|
||||
forConsumer.release();
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.scaler.producerconsumer;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -12,7 +12,7 @@ public class Runner {
|
||||
private static final Set<String> consumerNames = Set.of("c1", "c2", "c3", "c4");
|
||||
|
||||
public static void main(String[] args) {
|
||||
Queue<UnitOfWork> store = new ArrayDeque<>();
|
||||
Queue<UnitOfWork> store = new ConcurrentLinkedDeque<>();
|
||||
int maxSize = 20;
|
||||
|
||||
Semaphore forProducer = new Semaphore(maxSize);
|
||||
|
Loading…
x
Reference in New Issue
Block a user