-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLift.java
More file actions
50 lines (46 loc) · 1.83 KB
/
Copy pathLift.java
File metadata and controls
50 lines (46 loc) · 1.83 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
import java.util.concurrent.BlockingQueue;
public class Lift extends Thread {
private final BlockingQueue<Query> queries;
private int currentFloor = 1;
private Query query = null;
public Lift(String name, BlockingQueue<Query> queries) {
super(name);
this.queries = queries;
}
@Override
public void run() {
try {
while (true) {
if (query == null) {
query = queries.take();
}
if (query.getDestination() > currentFloor) {
Thread.sleep(1000);
currentFloor++;
System.out.println(getName() + ": " + (currentFloor - 1) + " -> " + currentFloor);
} else if (query.getDestination() < currentFloor) {
Thread.sleep(1000);
currentFloor--;
System.out.println(getName() + ": " + (currentFloor + 1) + " -> " + currentFloor);
} else {
Thread.sleep(250);
System.out.println(getName() + ": Doors opened");
Thread.sleep(500);
System.out.println(getName() + ": Doors closed");
if (query.isStarted()) {
if (query.getDestination() > 1) {
query = new Query(query.getDestination(), 1);
} else {
query = null;
}
} else {
query.start();
System.out.println(getName() + ": Starts " + query.getDescription());
}
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}