ΠΠΎΠΎΠ±ΡΠ΅ΠΌ ΡΠΈΡΠ°Ρ ΠΊΠ½ΠΈΠ³Ρ ΠΈ Π² Π½Π΅ΠΉ Π΅ΡΡΡ Π½Π΅Π±ΠΎΠ»ΡΡΠ°Ρ Π³Π»Π°Π²Π° ΠΏΡΠΎ ΠΌΠ½ΠΎΠ³ΠΎΠΏΠΎΡΠΎΡΠ½ΠΎΡΡΡ, ΡΠ°ΠΊ Π²ΠΎΡ Ρ ΡΡΠΎΠ»ΠΊΠ½ΡΠ»ΡΡ Ρ ΠΎΠ΄Π½ΠΈΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΡΠΉ Π½Π΅ ΡΠΎΠ²ΡΠ΅ΠΌ ΠΌΠΎΠ³Ρ ΠΏΠΎΠ½ΡΡΡ, Π²ΠΎΡ Π΅Π³ΠΎ ΠΊΠΎΠ΄:
// Try This 12-1
// A simulation of a traffic light that uses
// an enumeration to describe the light's color.
// An enumeration of the colors of a traffic light.
enum TrafficLightColor {
RED, GREEN, YELLOW
}
// A computerized traffic light.
class TrafficLightSimulator implements Runnable {
private TrafficLightColor tlc; // holds the current traffic light color
private boolean stop = false; // set to true to stop the simulation
private boolean changed = false; // true when the light has changed
TrafficLightSimulator(TrafficLightColor init) {
tlc = init;
}
TrafficLightSimulator() {
tlc = TrafficLightColor.RED;
}
// Start up the light.
public void run() {
while(!stop) {
try {
switch(tlc) {
case GREEN:
Thread.sleep(10000); // green for 10 seconds
break;
case YELLOW:
Thread.sleep(2000); // yellow for 2 seconds
break;
case RED:
Thread.sleep(12000); // red for 12 seconds
break;
}
} catch(InterruptedException exc) {
System.out.println(exc);
}
changeColor();
}
}
// Change color.
synchronized void changeColor() {
switch(tlc) {
case RED:
tlc = TrafficLightColor.GREEN;
break;
case YELLOW:
tlc = TrafficLightColor.RED;
break;
case GREEN:
tlc = TrafficLightColor.YELLOW;
}
changed = true;
notify(); // signal that the light has changed
}
// Wait until a light change occurs.
synchronized void waitForChange() {
try {
while(!changed)
wait(); // wait for light to change
changed = false;
} catch(InterruptedException exc) {
System.out.println(exc);
}
}
// Return current color.
synchronized TrafficLightColor getColor() {
return tlc;
}
// Stop the traffic light.
synchronized void cancel() {
stop = true;
}
}
class TrafficLightDemo {
public static void main(String[] args) {
TrafficLightSimulator tl =
new TrafficLightSimulator(TrafficLightColor.GREEN);
Thread thrd = new Thread(tl);
thrd.start();
for(int i=0; i < 9; i++) {
System.out.println(tl.getColor());
tl.waitForChange();
}
tl.cancel();
}
}
Π’Π°ΠΊ Π²ΠΎΡ Π² ΡΠ΅ΠΌ ΡΡΡΡ Π²ΠΎΠΏΡΠΎΡΠ°, Π² ΠΌΠ΅ΡΠΎΠ΄Π΅ synchronized void waitForChange() Π΅ΡΡΡ ΡΠΈΠΊΠ» while ΠΊΠΎΡΠΎΡΡΠΉ Π½Π° Π²Ρ
ΠΎΠ΄ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ !changed ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ΠΎΠ½Π° false ΡΠ°ΠΌ ΠΎΠ½Π° Ρ Π²ΠΎΡΠΊΠ»ΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΡΠΌ Π·Π½Π°ΠΊΠΎΠΌ , Π° Π·Π½Π°ΡΠΈΡ ΡΡΠΎ Π½Π΅ false ΡΠΎ true ΠΈ ΡΠΈΠΊΠ» Π·Π°ΠΏΡΡΡΠΈΡΡΡ ΠΈ ΠΏΡΠΈΠΎΡΡΠ°Π½ΠΎΠ²ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΏΠΎΡΠΎΠΊΠ° main(ΡΠ°ΠΊ Ρ ΡΠ΅Π±Π΅ ΡΡΠΎ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΡ) ΠΈΠ΄Π΅ΠΌ Π΄Π°Π»ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅ ΠΏΠΎΠ΄ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ΠΌ thrd Π΅ΡΡΡ ΠΌΠ΅ΡΠΎΠ΄ synchronized void changeColor() ΠΊΠΎΡΠΎΡΡΠΉ Π² ΡΠ°ΠΌΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ ΡΡΠΎΠΉ ΠΆΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ changed ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ true ΠΈ Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΠΌΠ΅ΡΠΎΠ΄ notify() ΠΊΠΎΡΠΎΡΡΠΉ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π·Π°ΠΏΡΡΡΠΈΡΡ ΠΏΠΎΡΠΎΠΊ main, ΠΊΠΎΡΠΎΡΡΠΉ Π·Π°Π½ΠΎΠ²ΠΎ Π·Π°ΠΏΡΡΡΠΈΡ ΠΌΠ΅ΡΠΎΠ΄ synchronized void waitForChange() Π½ΠΎ Ρ Π½Π΅ ΠΌΠΎΠ³Ρ ΠΏΠΎΠ½ΡΡΡ ΠΊΠ°ΠΊ Π² Π½Π΅ΠΌ Π·Π°ΠΏΡΡΡΠΈΡΡΡ ΡΠΈΠΊΠ» while ΠΊΠΎΡΠΎΡΡΠΉ ΡΠ½ΠΎΠ²Π° ΠΏΡΠΈΠΎΡΡΠ°Π½ΠΎΠ²ΠΈΡ ΠΏΠΎΡΠΎΠΊ main Π΅ΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ changed ΠΏΡΠΈΡΠ²ΠΎΠΈΠ»ΠΈ true , Π° tru Ρ Π²ΠΎΡΠΊΠ»ΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΡΠΌ Π·Π½Π°ΠΊΠΎΠΌ ΡΡΠΎ false?
Π£ΠΆΠ΅ ΠΏΠ°ΡΡ Π½ΠΎΡΠ΅ΠΉ Π½Π΅ ΠΌΠΎΠ³Ρ ΠΏΠΎΠ½ΡΡΡ ΠΊΠ°ΠΊ ΡΡΠΎΡ ΠΊΠΎΠ΄ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ