4 * $Id: Troll.java,v 1.1 2008/05/08 18:33:30 slf4878 Exp slf4878 $
8 * Revision 1.1 2008/05/08 18:33:30 slf4878
9 * Now controls order of Woolies.
11 * Revision 2.3 2006/10/17 18:48:23 vcss232
14 * Revision 2.2 2005/01/26 03:19:36 vcss232
17 * Revision 2.1 2004/04/12 23:43:53 cs3
18 * moved the instance variables to the top of the class
21 * Revision 2.0 2004/01/20 22:06:14 icss235
22 * *** empty log message ***
29 * A troll to guard a bridge. The troll simply passes requests from
30 * woolies to his bridge. It is a placeholder for a later exercise.
34 * <li>The troll gets entry permission; the woolie itself tells
35 * the bridge it is off.</li>
36 * <li>The troll runs an independent thread to pull things off
37 * its queue. This thread runs forever, so the Troll objects
38 * must be a daemon thread.</li>
42 * @author Sarah Frisco
48 * The bridge that this troll guards. The Bridge object
49 * creates the troll object.
51 private Bridge myBridge
;
52 //A queue to keep track of the woolies trying to get on the bridge]
54 private WoolieQueue queue
;
57 * Create a troll. (Called by its Bridge object.)
59 * @param b the Bridge object associated with this Troll object
61 public Troll( Bridge b
) {
63 //Creates its own woolie queue
64 queue
= new WoolieQueue();
68 * Request permission to go on the bridge. This method is
69 * called by a Woolie object. It just calls Bridge.enter().
72 * <li>The woolie got permission and has entered the bridge.
75 public synchronized void enterBridgePlease(Woolie woolie
) {
76 System
.out
.println(woolie
.getName() + " is talking to the troll");
79 //If the bridge is not full, and the woolie trying to get in is the next
80 //woolie in line, it allows the first woolie in the queue
81 //to get on the bridge. It also removes that woolie from
82 //the queue May 8,08 slf4878
85 if (!myBridge
.isFull() && (woolie
== queue
.peek())) {
92 } catch (InterruptedException e
) {};
99 * Inform the troll that the caller (the woolie) is getting
102 synchronized public void leave() {
103 myBridge
.removeWoolie();