Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@
import race.Road;
import race.Tunnel;

import java.util.concurrent.CountDownLatch;

public class MainClass {
public static final int CARS_COUNT = 4;
public static void main(String[] args) {

public static CountDownLatch carsReadiness = new CountDownLatch(CARS_COUNT);
public static CountDownLatch notFinishedCars = new CountDownLatch(CARS_COUNT);

public static void main(String[] args) throws InterruptedException {
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Подготовка!!!");
Race race = new Race(new Road(60), new Tunnel(), new Road(40));

Race race = new Race(new Road(60), new Tunnel(CARS_COUNT/2), 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));
cars[i] = new Car(race, 20 + (int) (Math.random() * 10), carsReadiness, notFinishedCars);
}

for (int i = 0; i < cars.length; i++) {
new Thread(cars[i]).start();
}

carsReadiness.await();

System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка началась!!!");
notFinishedCars.await();
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!");
}
}
18 changes: 16 additions & 2 deletions src/race/Car.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package race;

import java.security.cert.Extension;
import java.util.concurrent.CountDownLatch;

public class Car implements Runnable {
private static int CARS_COUNT;
static {
CARS_COUNT = 0;
}

private CountDownLatch carsReadiness;
private CountDownLatch notFinishedCars;

private Race race;
private int speed;
private String name;


public String getName() {
return name;
}
Expand All @@ -20,10 +24,14 @@ public int getSpeed() {
return speed;
}

public Car(Race race, int speed) {
public Car(Race race, int speed, CountDownLatch carsReadiness, CountDownLatch notFinishedCars) {
this.race = race;
this.speed = speed;
this.carsReadiness = carsReadiness;
this.notFinishedCars = notFinishedCars;

CARS_COUNT++;

this.name = "Участник #" + CARS_COUNT;
}

Expand All @@ -33,11 +41,17 @@ public void run() {
System.out.println(this.name + " готовится");
Thread.sleep(500 + (int)(Math.random() * 800));
System.out.println(this.name + " готов");

carsReadiness.countDown();
carsReadiness.await();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < race.getStages().size(); i++) {
race.getStages().get(i).go(this);
}

race.finish(this);
notFinishedCars.countDown();
}
}
11 changes: 11 additions & 0 deletions src/race/Race.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@

public class Race {
private ArrayList<Stage> stages;
private int finished = 0;

public ArrayList<Stage> getStages() { return stages; }

public Race(Stage... stages) {
this.stages = new ArrayList<>(Arrays.asList(stages));
}

public synchronized void finish(Car car) {
finished++;

if (finished == 1) {
System.out.println(car.getName() + " - WIN");
} else {
System.out.println(car.getName() + " закончил гонку");
}
}
}
11 changes: 10 additions & 1 deletion src/race/Tunnel.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package race;

import java.util.concurrent.Semaphore;

public class Tunnel extends Stage {
public Tunnel() {
private Semaphore sph;

public Tunnel(int limit) {
this.length = 80;
this.description = "Тоннель " + length + " метров";
this.sph = new Semaphore(limit);
}

@Override
public void go(Car c) {
try {
try {
System.out.println(c.getName() + " готовится к этапу(ждет): " + description);
sph.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);

sph.release();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down