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
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
package hse.java.lectures.lecture6.tasks.queue;

import java.util.LinkedList;
import java.util.Queue;

public class BoundedBlockingQueue<T> {

private final Queue<T> q;
private final int capacity;
private int size;

public BoundedBlockingQueue(int capacity) {

if (capacity <= 0) {
throw new IllegalArgumentException();
}
this.capacity = capacity;
q = new LinkedList<T>();
size = 0;
}

public void put(T item) throws InterruptedException {

if (item == null) {
throw new NullPointerException();
}
synchronized (this) {
while (size == capacity) {
wait();
}
q.add(item);
size++;
notifyAll();
}
}

public T take() throws InterruptedException {
return null;
synchronized (this) {
while (size == 0) {
wait();
}
T item = q.poll();
size--;
notifyAll();
return item;
}
}

public int size() {
return 0;
public synchronized int size() {
return size;
}

public int capacity() {
return 0;
return capacity;
}
}
Loading