-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
33 lines (29 loc) · 1.37 KB
/
MainClass.java
File metadata and controls
33 lines (29 loc) · 1.37 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.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
public class MainClass {
public static final int CARS_COUNT = 4;
public static final int MAIN_THREAD = 1;
public static CountDownLatch endRace = new CountDownLatch(CARS_COUNT);
public static CyclicBarrier startRace = new CyclicBarrier(CARS_COUNT + MAIN_THREAD);
public static void main(String[] args) {
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Подготовка!!!");
Race race = new Race(new Road(60), new Tunnel(), new Road(40));
Car[] cars = new Car[CARS_COUNT];
for (int i = 0; i < cars.length; i++) {
cars[i] = new Car(race, 20 + (int) (Math.random() * 10));
}
for (int i = 0; i < cars.length; i++) {
new Thread(cars[i]).start();
}
try {
startRace.await();
System.out.println(); //Для удобства чтения в командной строке
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка началась!!!");
endRace.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!");
}
}