Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 17 additions & 8 deletions src/main/java/core/basesyntax/BlockingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@
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
public synchronized void put(T value) throws InterruptedException {
while (queue.size() == capacity) {
wait();
}

queue.offer(value);
notifyAll();
}

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

T value = queue.poll();
notifyAll();
return value;
}

public synchronized boolean isEmpty() {
// write your code here
return true;
return queue.isEmpty();
}
}
4 changes: 2 additions & 2 deletions src/main/java/core/basesyntax/thread/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import core.basesyntax.BlockingQueue;

public class Consumer implements Runnable {
private BlockingQueue<Integer> blockingQueue;
private final BlockingQueue<Integer> blockingQueue;

public Consumer(BlockingQueue<Integer> blockingQueue) {
this.blockingQueue = blockingQueue;
}

@Override
public void run() {
while (!blockingQueue.isEmpty()) {
while (true) {
try {
System.out.println("Took value " + blockingQueue.take());
} catch (InterruptedException e) {
Expand Down
Loading