Repository navigation
Expand file tree
/
Copy pathGate.java
More file actions
147 lines (119 loc) · 3.93 KB
/
Copy pathGate.java
File metadata and controls
147 lines (119 loc) · 3.93 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
public class Gate {
private final int gateNumber;
private boolean isAvailable = true;
private boolean cleaningCompleted = false;
private boolean embarkCompleted = false;
public Gate(int number) {
this.gateNumber = number;
}
public int getGateNumber() {
return gateNumber;
}
// Lock gate
public synchronized void occupy() {
isAvailable = false;
}
// Release gate & reset to default settings
public synchronized void release() {
isAvailable = true;
cleaningCompleted = false;
embarkCompleted = false;
}
// Check can assign not
public synchronized boolean isAvailable() {
return isAvailable;
}
// Check ready for take off not
public synchronized boolean operationCompleted() {
return cleaningCompleted && embarkCompleted;
}
// Start operation
public void openGate(Plane plane) {
DisembarkOperation disembarkTask = new DisembarkOperation(this, plane);
Thread disembarkThread = new Thread(disembarkTask, "Thread-Disembark");
CleaningOperation cleaningTask = new CleaningOperation(this, plane);
Thread cleaningThread = new Thread(cleaningTask, "Thread-Cleaning");
// Start concurrently, refuel start at another side
disembarkThread.start();
cleaningThread.start();
System.out.println(plane.getName() + ": Start cleaning and resupply.");
}
public void disembarkPassengers(Plane plane) throws InterruptedException {
// Disembark take 1 second
Thread.sleep(1000);
synchronized (this) {
System.out.println(plane.getName() + ": All " + plane.getnumPassengerDisembark() + " passengers disembarked.");
// Finish disembark, start embark passengers
EmbarkOperation embarkTask = new EmbarkOperation(this, plane);
Thread embarkThread = new Thread(embarkTask, "Thread-Embark");
embarkThread.start();
}
}
public void cleanPlane(Plane plane) throws InterruptedException {
// Cleaning takes 2 seconds
Thread.sleep(2000);
synchronized (this) {
cleaningCompleted = true;
System.out.println(plane.getName() + ": Cleaning and resupply completed.");
}
}
public void embarkPassengers(Plane plane) throws InterruptedException {
// Embark takes 1 second
Thread.sleep(1000);
synchronized (this) {
embarkCompleted = true;
System.out.println(plane.getName() + ": All " + plane.getnumPassengerEmbark() + " passengers embarked.");
}
}
}
class DisembarkOperation implements Runnable {
private final Gate gate;
private final Plane plane;
public DisembarkOperation(Gate gate, Plane plane) {
this.gate = gate;
this.plane = plane;
}
@Override
public void run() {
try {
gate.disembarkPassengers(plane);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class CleaningOperation implements Runnable {
private final Gate gate;
private final Plane plane;
public CleaningOperation(Gate gate, Plane plane) {
this.gate = gate;
this.plane = plane;
}
@Override
public void run() {
try {
gate.cleanPlane(plane);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class EmbarkOperation implements Runnable {
private final Gate gate;
private final Plane plane;
public EmbarkOperation(Gate gate, Plane plane) {
this.gate = gate;
this.plane = plane;
}
@Override
public void run() {
try {
gate.embarkPassengers(plane);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}