Skip to content

Instantly share code, notes, and snippets.

@deepshah22
Created October 15, 2020 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepshah22/1a1583baf1492411bc06de001365a39c to your computer and use it in GitHub Desktop.
Save deepshah22/1a1583baf1492411bc06de001365a39c to your computer and use it in GitHub Desktop.
import java.util.Deque;
import java.util.LinkedList;
public class JavaBoundedBlockingQueue {
Deque<Integer> deQueue;
int size;
Object lock;
public JavaBoundedBlockingQueue(int capacity) {
deQueue = new LinkedList<>();
size = capacity;
lock = new Object();
}
public static void main(String[] args) throws InterruptedException {
JavaBoundedBlockingQueue boundedBlockingQueue = new JavaBoundedBlockingQueue(3);
boundedBlockingQueue.enqueue(1);
boundedBlockingQueue.dequeue();
boundedBlockingQueue.dequeue();
boundedBlockingQueue.enqueue(0);
boundedBlockingQueue.enqueue(2);
boundedBlockingQueue.enqueue(3);
boundedBlockingQueue.enqueue(4);
}
// When queue is full,
// block enqueue thread,
// add thread to full waiting list
private void enqueue(int element) throws InterruptedException {
synchronized (lock) {
while (deQueue.size() == size) {
lock.wait();
}
deQueue.addLast(element);
lock.notify();
}
}
// When queue is empty, we block dequeue thread,
// and add thread to empty waiting list
private int dequeue() throws InterruptedException {
int val = 0;
synchronized (lock) {
while (deQueue.isEmpty()) {
lock.wait();
}
val = deQueue.removeFirst();
lock.notify();
}
return val;
}
public int size() {
return deQueue.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment