Source of CompetingPoets.java


  1: //CompetingPoets.java

  3: import java.applet.Applet;
  4: import java.awt.*;

  6: public class CompetingPoets extends Applet
  7: {
  8:     public void init()
  9:     {
 10:         TextArea display =
 11:             new TextArea("", 40, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
 12:         add(display);

 14:         ScreenController screen = new ScreenController();
 15:         Nursery nursery = new Nursery(display, screen);
 16:         Revolutionary revolutionary = new Revolutionary(display, screen);
 17:         Mowing mowing = new Mowing(display, screen);
 18:         nursery.start();
 19:         revolutionary.start();
 20:         mowing.start();
 21:     }
 22: }


 25: //This class illustrates the "mutual exclusion" mechanism.
 26: //The value of "inuse" is accessed by both methods, but
 27: //the methods are "synchronized". This means any thread
 28: //trying to enter any synchonized method in any object while
 29: //any such method is in used is "blocked" and must wait till
 30: //the first thread has exited from that method.
 31: class ScreenController
 32: {
 33:     private boolean inUse = false;

 35:     public synchronized void askFor()
 36:     {
 37:         while (inUse)
 38:             try
 39:             {
 40:                 wait();
 41:             }
 42:             catch(InterruptedException e)
 43:             {
 44:                 System.err.println("Exception");
 45:             }
 46:         inUse = true;
 47:     }

 49:     public synchronized void relinquish()
 50:     {
 51:         inUse = false;
 52:         notify();
 53:     }
 54: }

 56: class Mowing extends Thread
 57: {
 58:     private TextArea display;
 59:     private ScreenController screen; 

 61:     public Mowing(TextArea display, ScreenController screen)
 62:     {
 63:         this.display = display;
 64:         this.screen = screen;
 65:     }

 67:     public void run()
 68:     {
 69:         while (true)
 70:         {
 71:             screen.askFor();
 72:             display.append("One man went to mow\n");
 73:             display.append("Went to mow a meadow\n\n");
 74:             screen.relinquish();
 75:             try
 76:             {
 77:                 Thread.sleep(4000);
 78:             }
 79:             catch (InterruptedException e)
 80:             {
 81:                 display.append ("Sleep exception\n");
 82:             }
 83:         }
 84:     }
 85: }

 87: class Nursery extends Thread
 88: {
 89:     private TextArea display; 
 90:     private ScreenController screen;

 92:     public Nursery(TextArea display, ScreenController screen)
 93:     {
 94:         this.display = display;
 95:         this.screen = screen;
 96:     }

 98:     public void run()
 99:     {
100:         while (true)
101:         {
102:             screen.askFor();
103:             display.append("Mary had a little lamb\n");
104:             display.append("Its fleece was white as snow\n\n");
105:             screen.relinquish();
106:             try
107:             {
108:                 Thread.sleep(6000);
109:             }
110:             catch (InterruptedException e)
111:             {
112:                 display.append ("Sleep exception\n");
113:             }
114:         }
115:     }
116: }

118: class Revolutionary extends Thread
119: {
120:     private TextArea display; 
121:     private ScreenController screen;

123:     public Revolutionary(TextArea display, ScreenController screen)
124:     {
125:         this.display = display;
126:         this.screen = screen;
127:     }

129:     public void run()
130:     {
131:         while (true)
132:         {
133:             screen.askFor();
134:             display.append("Praise Marx\n");
135:             display.append("And pass the ammunition\n\n");
136:             screen.relinquish();
137:             try
138:             {
139:                 Thread.sleep(8000);
140:             }
141:             catch (InterruptedException e)
142:             {
143:                 display.append ("Sleep exception\n");
144:             }
145:         }
146:     }
147: }