-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
51 lines (43 loc) · 1.35 KB
/
Car.java
File metadata and controls
51 lines (43 loc) · 1.35 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
public class Car implements Runnable {
public static final int MAIN_THREAD_IS_FIRST = 1;
private static int CARS_COUNT;
private static int place = 0;
static {
CARS_COUNT = 0;
}
private Race race;
private int speed;
private String name;
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public Car(Race race, int speed) {
this.race = race;
this.speed = speed;
CARS_COUNT++;
this.name = "Участник #" + CARS_COUNT;
}
@Override
public void run() {
try {
System.out.println(this.name + " готовится");
Thread.sleep(500 + (int) (Math.random() * 800));
System.out.println(this.name + " готов");
MainClass.startRace.await();
Thread.sleep(MAIN_THREAD_IS_FIRST);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < race.getStages().size(); i++) {
race.getStages().get(i).go(this);
if (i == race.getStages().size()-1) {
MainClass.endRace.countDown();
if (++place == 1) System.out.println(this.name + " WIN");
else System.out.println(this.name + " знанял " + place + " место");
}
}
}
}