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

public class BoundedBlockingQueue<T> {


public BoundedBlockingQueue(int capacity) {

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

public void put(T item) throws InterruptedException {

}

public T take() throws InterruptedException {
return null;
}

public int size() {
return 0;
}
public class BoundedBlockingQueue<T> {

public int capacity() {
return 0;
}
private final int capacity;
private Queue<T> queue = new LinkedList<T>();

public BoundedBlockingQueue(int capacity) {
if (capacity <= 0){
throw new IllegalArgumentException();
}
this.capacity = capacity;
}

public synchronized void put(T item) throws InterruptedException {
if (item == null){
throw new IllegalArgumentException();
}

while (queue.size() == capacity) wait();
queue.offer(item);
notifyAll();
}

public synchronized T take() throws InterruptedException {
while(queue.isEmpty()) wait();
T item = queue.poll();
notifyAll();
return item;
}

public synchronized int size() {
return queue.size();
}

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