public class CompetingPoets extends Applet
class ScreenController
class Mowing extends Thread
class Nursery extends Thread
class Revolutionary extends Thread
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("", 10, 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 "synchonized". 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(2000);
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(2000);
109: }
110: catch (InterruptedException e)
111: {
112: display.append ("Sleep exception\n");
114: }
115: }
116: }
117: }
119: class Revolutionary extends Thread
120: {
121: private TextArea display;
122: private ScreenController screen;
124: public Revolutionary(TextArea display, ScreenController screen)
125: {
126: this.display = display;
127: this.screen = screen;
128: }
130: public void run()
131: {
132: while (true)
133: {
134: screen.askFor();
135: display.append("Praise Marx\n");
136: display.append("and pass the ammunition\n\n");
137: screen.relinquish();
138: try
139: {
140: Thread.sleep(2000);
141: }
142: catch (InterruptedException e)
143: {
144: display.append ("Sleep exception\n");
145: }
146: }
147: }
148: }