lajdlksadlmla
[rmh3093.git] / lab9 / act2 / WoolieQueue.java
blob2c834624236957008b83d7d2982b35836abb5e51
1 /*
2 * WoolieQueue.java
3 * $Id: WoolieQueue.java,v 2.4 2005/01/26 04:15:44 vcss232 Exp $
5 * Revisions:
6 * $Log: WoolieQueue.java,v $
7 * Revision 2.4 2005/01/26 04:15:44 vcss232
8 * Added generics (jeh)
10 * Revision 2.3 2004/04/12 23:49:05 cs3
11 * moved the instance variables to the top of the class (bk)
13 * Revision 2.2 2004/01/21 20:05:08 icss235
14 * Reversed order from LIFO to FIFO (jeh)
16 * Revision 2.1 2004/01/21 16:43:43 icss235
17 * Simplified design. (jeh,swm)
19 * Revision 2.0 2004/01/20 22:06:59 icss235
20 * *** empty log message ***
24 /**
25 * A multithread-safe wrapper around a linked list to make a queue.
27 * @author James Heliotis
29 public class WoolieQueue {
31 /**
32 * The underlying data structure
34 private java.util.LinkedList< Woolie > woolies =
35 new java.util.LinkedList< Woolie >();
38 /**
39 * Constructor. Does nothing in this version.
41 public WoolieQueue() {
44 /**
45 * Insert a woolie into the queue.
47 * @param caller the Woolie to be inserted.
49 public synchronized void insert( Woolie caller ) {
50 woolies.addFirst( caller );
51 notifyAll();
54 /**
55 * Inspect the first woolie in the queue.
56 * This method never blocks.
58 * @return the Woolie that has been in the queue for
59 * the longest time, or null if the queue is
60 * empty.
62 public synchronized Woolie peek() {
63 Woolie result = null;
64 try {
65 result = woolies.getLast();
67 catch( java.util.NoSuchElementException nse ) {
69 return result;
72 /**
73 * Remove a woolie from the queue.
74 * This call blocks while the queue is empty.
76 * @return the Woolie that has been in the queue for
77 * the longest time.
79 public synchronized Woolie remove() {
80 while ( woolies.isEmpty() ) {
81 try {
82 wait();
84 catch( InterruptedException ie ) {}
86 return woolies.removeLast();