-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTunnel.java
More file actions
33 lines (27 loc) · 1.04 KB
/
Tunnel.java
File metadata and controls
33 lines (27 loc) · 1.04 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
import java.util.concurrent.Semaphore;
public class Tunnel extends Stage {
private static Semaphore semaphore = new Semaphore(MainClass.CARS_COUNT / 2);
public Tunnel() {
this.length = 80;
this.description = "Тоннель " + length + " метров";
}
@Override
public void go(Car c) {
try {
try {
if (semaphore.availablePermits() == 0)
System.out.println(c.getName() + " готовится к этапу(ждет): " + description);
semaphore.acquire();
System.out.println(c.getName() + " начал этап: " + description);
Thread.sleep(length / c.getSpeed() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(c.getName() + " закончил этап: " + description);
semaphore.release();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}