-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficSignal.java
More file actions
55 lines (41 loc) · 1.42 KB
/
Copy pathTrafficSignal.java
File metadata and controls
55 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import javax.management.RuntimeErrorException;
public class TrafficSignal {
public enum TrafficColor{
Red(9000),
Yellow(1000),
Green(3000);
private final int onTimeInMillis;
public int getOnTimeInMillis() {
return onTimeInMillis;
}
TrafficColor(int onTimeInMillis){
this.onTimeInMillis = onTimeInMillis;
}
}
public static class TrafficLightThread extends Thread{
private final TrafficColor color;
public TrafficLightThread(TrafficColor color){
this.color = color;
}
@Override
public void run() {
System.out.printf("%s active\n", color);
try {
Thread.sleep(color.getOnTimeInMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.printf("%s Inactive\n", color);
}
}
public static void main(String[] args) throws InterruptedException {
TrafficLightThread red = new TrafficLightThread(TrafficColor.Red);
TrafficLightThread Yellow = new TrafficLightThread(TrafficColor.Yellow);
TrafficLightThread Green = new TrafficLightThread(TrafficColor.Green);
Green.start();
Green.join();
Yellow.start();
Yellow.join();
red.start();
}
}