Concurrency
Last updated
Was this helpful?
Last updated
Was this helpful?
Deque<Integer> d;
int size;
Object lock;
public BoundedBlockingQueue(int capacity) {
d = new LinkedList<>();
size = capacity;
lock = new Object();
}
public void enqueue(int element) throws InterruptedException {
synchronized (lock) {
while (d.size() == size) {
lock.wait();
}
d.addLast(element);
lock.notify();
}
}
public int dequeue() throws InterruptedException {
int val = 0;
synchronized (lock) {
while (d.isEmpty()) lock.wait();
val = d.removeFirst();
lock.notify();
}
return val;
}
public int size() {
return d.size();
}