lajdlksadlmla
[rmh3093.git] / lab9 / act2 / Troll.java
blob64fcdb73fc33560e2a276697cc80d96d83789f38
1 /*
2 * Troll.java
4 * $Id: Troll.java,v 1.1 2008/05/08 18:33:30 slf4878 Exp slf4878 $
6 * Revisions:
7 * $Log: Troll.java,v $
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
12 * test
14 * Revision 2.2 2005/01/26 03:19:36 vcss232
15 * removed tabs (jeh)
17 * Revision 2.1 2004/04/12 23:43:53 cs3
18 * moved the instance variables to the top of the class
19 * (bk)
21 * Revision 2.0 2004/01/20 22:06:14 icss235
22 * *** empty log message ***
28 /**
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.
31 * <p>
32 * Note
33 * <ul>
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>
39 * <ul>
41 * @author Unknown
42 * @author Sarah Frisco
43 * @author Ryan Hope
45 public class Troll {
47 /**
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]
53 //May 8, 08 slf4878
54 private WoolieQueue queue;
56 /**
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 ) {
62 myBridge = b;
63 //Creates its own woolie queue
64 queue = new WoolieQueue();
67 /**
68 * Request permission to go on the bridge. This method is
69 * called by a Woolie object. It just calls Bridge.enter().
71 * Postcondition:<ul>
72 * <li>The woolie got permission and has entered the bridge.
73 * </ul>
75 public synchronized void enterBridgePlease(Woolie woolie) {
76 System.out.println(woolie.getName() + " is talking to the troll");
77 queue.insert(woolie);
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
84 while (true) {
85 if (!myBridge.isFull() && (woolie == queue.peek())) {
86 myBridge.addWoolie();
87 queue.remove();
88 break;
89 } else {
90 try {
91 wait();
92 } catch (InterruptedException e) {};
98 /**
99 * Inform the troll that the caller (the woolie) is getting
100 * off the bridge.
102 synchronized public void leave() {
103 myBridge.removeWoolie();
104 notifyAll();