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
14 changes: 14 additions & 0 deletions task5/src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import java.util.concurrent.CyclicBarrier;

public class Car implements Runnable {
private static int CARS_COUNT;
private static CyclicBarrier cb;
private static String winner;
static {
CARS_COUNT = 0;
}
Expand All @@ -17,18 +21,28 @@ public Car(Race race, int speed) {
this.speed = speed;
CARS_COUNT++;
this.name = "Участник #" + CARS_COUNT;
cb = new CyclicBarrier(CARS_COUNT);
}


@Override
public void run() {
try {
System.out.println(this.name + " готовится");
Thread.sleep(500 + (int)(Math.random() * 800));
MainClass.preparedCars.countDown();
System.out.println(this.name + " готов");
cb.await();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < race.getStages().size(); i++) {
race.getStages().get(i).go(this);
}
if (winner == null) {
winner = this.name;
System.out.println(this.name + " - WIN!!!");
}
MainClass.finishedCars.countDown();
}
}
16 changes: 15 additions & 1 deletion task5/src/main/java/MainClass.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import java.util.concurrent.CountDownLatch;

public class MainClass {

public static final int CARS_COUNT = 4;
public static final CountDownLatch preparedCars = new CountDownLatch(CARS_COUNT);
public static final CountDownLatch finishedCars = new CountDownLatch(CARS_COUNT);

public static void main(String[] args) {
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));
}
for (int i = 0; i < cars.length; i++) {
new Thread(cars[i]).start();
}
try {
preparedCars.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка началась!!!");
try {
finishedCars.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!");
}
}
8 changes: 7 additions & 1 deletion task5/src/main/java/Tunnel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import java.util.concurrent.Semaphore;

public class Tunnel extends Stage {
public Tunnel() {
private Semaphore tunnelVolume;
public Tunnel(int volume) {
this.tunnelVolume = new Semaphore(volume);
this.length = 80;
this.description = "Тоннель " + length + " метров";
}
Expand All @@ -8,12 +12,14 @@ public void go(Car c) {
try {
try {
System.out.println(c.getName() + " готовится к этапу(ждет): " + description);
tunnelVolume.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);
tunnelVolume.release();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down