| BlockingQueue.java |
package queue;
import java.util.LinkedList;
/**
* Blocking Queue data structure
* @author Winston Prakash
*/
public class BlockingQueue<E>{
private final Queue queue = new LinkedListQueue();
/**
* Check if the queue is empty
*/
public boolean isEmpty() {
return queue.isEmpty();
}
/**
* This method pushes the item to the end of the
* queue and then notifies one of the thread
*/
public void enqueue(E item){
synchronized(queue){
queue.enqueue(item);
queue.notify();
}
}
/**
* Get the item from the head of the queue
* This operation blocks until either an item is returned
* or the thread is interrupted, in which case it throws an
* InterruptedException.
*/
public E dequeue() throws InterruptedException {
synchronized(queue) {
while (queue.isEmpty()) {
queue.wait();
}
return (E) queue.dequeue();
}
}
/**
* Get the size of the queue
*/
public int size() {
return queue.size();
}
}