import java.util.*;
/**
* Abstract base class for queue implementations that keep their queue in
* order of removal
*/
public abstract class AbstractQueue implements Queue {
protected Vector queue = new Vector();
/**
* Create a new queue.
*/
public AbstractQueue() {
}
/**
* Insert an object in the queue, in the correct relative position. */
public abstract Object put(Object obj);
/**
* Remove from the beginning of the queue. Does not return until
* an object appears in the queue and becomes available to this thread.
* @return the item formerly at the head of the queue, or null if a
* timeout occurred before an item was available.
* @throws InterruptedException if interrupted while waiting
*/
public synchronized Object get(Deadline timer) throws InterruptedException {
Object head = peekWait(timer);
if (head != null) {
queue.removeElementAt(0);
}
return head;
}
/**
* Wait until the queue is non-empty, then return the element at the
* head of the queue, leaving it on the queue.
* @return the item at the head of the queue, or null if a
* timeout occurred before an item was available.
* @throws InterruptedException if interrupted while waiting
*/
public synchronized Object peekWait(Deadline timer)
throws InterruptedException {
final Thread thread = Thread.currentThread();
Deadline.Callback cb = new Deadline.Callback() {
public void changed(Deadline deadline) {
thread.interrupt();
}};
try {
timer.registerCallback(cb);
while (queue.isEmpty() && !timer.expired()) {
this.wait(timer.getSleepTime());
}
} finally {
timer.unregisterCallback(cb);
}
if (!queue.isEmpty()) {
// remove from beginning
Object obj = queue.firstElement();
return obj;
} else {
return null;
}
}
/**
* Return first element on queue, without removing it.
* @return The element at the head of the queue, or null if queue is empty
*/
public synchronized Object peek() {
return (queue.isEmpty() ? null : queue.firstElement());
}
/**
* Remove the specified element from the queue. If the element appears
* in the queue more than once, the behavior is undefined.
* @return true iff the element was present in the queue
*/
public synchronized boolean remove(Object obj) {
return queue.remove(obj);
}
/**
* Return the number of elements in the queue
*/
public int size() {
return queue.size();
}
/**
* Return true iff the queue is empty
*/
public boolean isEmpty() {
return queue.isEmpty();
}
/**
* Return a snapshot of the queue contents
*/
public synchronized List copyAsList() {
return new ArrayList(queue);
}
}
import java.util.*;
/**
* A thread-safe FIFO queue
*/
public class FifoQueue extends AbstractQueue {
/**
* Create a new FIFO queue.
*/
public FifoQueue() {
super();
}
/**
* Add to the end of the queue. */
public synchronized Object put(Object obj) {
if (obj == null) {
throw new NullPointerException(“Attempt to put null element on Queue”);
}
queue.addElement(obj);
// supposedly allows the highest priority thread to run first
notifyAll();
return obj;
}
}
import java.util.*;
/**
* Queue interface.
*/
public interface Queue {
/**
* Add to the queue. */
public Object put(Object obj);
/**
* Remove first element from the queue. Does not return until
* an object appears in the queue or the timer expires.
* @return The element formerly at the head of the queue
*/
public Object get(Deadline timer) throws InterruptedException;
/**
* Return first element on queue, without removing it.
* @return The element at the head of the queue, or null if queue is empty
*/
public Object peek();
/**
* Wait until the queue is non-empty, then return the element at the
* head of the queue, leaving it on the queue.
* @return the item at the head of the queue, or null if a
* timeout occurred before an item was available.
* @throws InterruptedException if interrupted while waiting
*/
public Object peekWait(Deadline timer) throws InterruptedException;
/**
* Remove the specified element from the queue. If the element appears
* in the queue more than once, the behavior is undefined.
* @return true iff the element was present in the queue
*/
public boolean remove(Object obj);
/**
* Return the number of elements in the queue
*/
public int size();
/**
* Return true iff the queue is empty
*/
public boolean isEmpty();
/**
* Return a snapshot of the queue contents
*/
public List copyAsList();
}