-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevatorController.java
More file actions
32 lines (29 loc) · 1.03 KB
/
Copy pathElevatorController.java
File metadata and controls
32 lines (29 loc) · 1.03 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
import java.util.ArrayList;
import java.util.Scanner;
public class ElevatorController implements Runnable {
ArrayList<SingleElevator> elevators;
ElevatorController(int n) {
elevators = new ArrayList<>();
for (int i = 0; i < n; i++) elevators.add(new SingleElevator(i+1));
}
public void run() {
for (SingleElevator elevator : elevators) {
Thread nThread = new Thread(elevator);
nThread.start();
}
while(true) {
try {
Scanner in = new Scanner(System.in);
int floor = in.nextInt();
int destination = in.nextInt();
// For now, only the first elevator is used. Any load balancing
// can be implemented here, i.e. round robin, selecting
// a random elevator, or something more sophisticated.
elevators.get(0).call(floor, destination);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}