diff --git a/task5/src/main/java/Car.java b/task5/src/main/java/Car.java index b487609..b306f04 100644 --- a/task5/src/main/java/Car.java +++ b/task5/src/main/java/Car.java @@ -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; } @@ -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(); } } \ No newline at end of file diff --git a/task5/src/main/java/MainClass.java b/task5/src/main/java/MainClass.java index 4f50321..31b3983 100644 --- a/task5/src/main/java/MainClass.java +++ b/task5/src/main/java/MainClass.java @@ -1,10 +1,14 @@ +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)); @@ -12,7 +16,17 @@ public static void main(String[] args) { 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("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!"); } } \ No newline at end of file diff --git a/task5/src/main/java/Tunnel.java b/task5/src/main/java/Tunnel.java index 476d706..7f8d201 100644 --- a/task5/src/main/java/Tunnel.java +++ b/task5/src/main/java/Tunnel.java @@ -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 + " метров"; } @@ -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();