Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/main/java/core/basesyntax/BlockingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@
import java.util.Queue;

public class BlockingQueue<T> {
private Queue<T> queue = new LinkedList<>();
private int capacity;
private final Queue<T> queue = new LinkedList<>();
private final int capacity;

public BlockingQueue(int capacity) {
this.capacity = capacity;
}

public synchronized void put(T element) throws InterruptedException {
// write your code here
while (queue.size() == capacity) {
wait();
}
queue.add(element);
notifyAll();
}

public synchronized T take() throws InterruptedException {
// write your code here
return null;
while (isEmpty()) {
wait();
}
T value = queue.poll();
notifyAll();
return value;
}

public synchronized boolean isEmpty() {
// write your code here
return true;
boolean isEmpty = queue.isEmpty();
notifyAll();
return isEmpty;
}
}