Skip to content
Open
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
39 changes: 29 additions & 10 deletions src/main/java/core/basesyntax/BlockingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@
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 Object lock = new Object();
private final int capacity;

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

public synchronized void put(T element) throws InterruptedException {
// write your code here
public void put(T element) throws InterruptedException {
synchronized (lock) {
while (isFull()) {
lock.wait();
}
queue.add(element);
lock.notify();
}
}

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

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

public boolean isFull() {
synchronized (lock) {
return queue.size() == capacity;
}
}
}
Loading