From 7222a6a6c7298e08e003cbbe16f120942591e682 Mon Sep 17 00:00:00 2001 From: sinister524 Date: Thu, 21 Nov 2019 17:17:45 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D1=85=D1=80=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=B0=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=D0=BE=D0=B1=D0=B8=D0=BB=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=B8=D1=85=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4=20=D1=87?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B7=20=D1=82=D1=83=D0=BD=D0=B5=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task5/src/main/java/Car.java | 5 +++++ task5/src/main/java/MainClass.java | 4 ++-- task5/src/main/java/Tunnel.java | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/task5/src/main/java/Car.java b/task5/src/main/java/Car.java index b487609..69ea088 100644 --- a/task5/src/main/java/Car.java +++ b/task5/src/main/java/Car.java @@ -1,5 +1,8 @@ +import java.util.concurrent.CyclicBarrier; + public class Car implements Runnable { private static int CARS_COUNT; + private static CyclicBarrier cb; static { CARS_COUNT = 0; } @@ -17,6 +20,7 @@ public Car(Race race, int speed) { this.speed = speed; CARS_COUNT++; this.name = "Участник #" + CARS_COUNT; + cb = new CyclicBarrier(CARS_COUNT); } @Override public void run() { @@ -24,6 +28,7 @@ public void run() { System.out.println(this.name + " готовится"); Thread.sleep(500 + (int)(Math.random() * 800)); System.out.println(this.name + " готов"); + cb.await(); } catch (Exception e) { e.printStackTrace(); } diff --git a/task5/src/main/java/MainClass.java b/task5/src/main/java/MainClass.java index 4f50321..88b0aaf 100644 --- a/task5/src/main/java/MainClass.java +++ b/task5/src/main/java/MainClass.java @@ -4,7 +4,7 @@ public class MainClass { 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)); @@ -13,6 +13,6 @@ public static void main(String[] args) { new Thread(cars[i]).start(); } System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка началась!!!"); - System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!"); +// 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(); From 82a045f359fa90ae2294934ab0e8abc205369d24 Mon Sep 17 00:00:00 2001 From: sinister524 Date: Thu, 21 Nov 2019 20:06:14 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=97=20=E2=84=96=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task5/src/main/java/Car.java | 9 +++++++++ task5/src/main/java/MainClass.java | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/task5/src/main/java/Car.java b/task5/src/main/java/Car.java index 69ea088..b306f04 100644 --- a/task5/src/main/java/Car.java +++ b/task5/src/main/java/Car.java @@ -3,6 +3,7 @@ public class Car implements Runnable { private static int CARS_COUNT; private static CyclicBarrier cb; + private static String winner; static { CARS_COUNT = 0; } @@ -22,11 +23,14 @@ public Car(Race race, int speed) { 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) { @@ -35,5 +39,10 @@ public void run() { 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 88b0aaf..31b3983 100644 --- a/task5/src/main/java/MainClass.java +++ b/task5/src/main/java/MainClass.java @@ -1,6 +1,10 @@ +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("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Подготовка!!!"); @@ -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("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка началась!!!"); -// System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!"); + try { + finishedCars.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("ВАЖНОЕ ОБЪЯВЛЕНИЕ >>> Гонка закончилась!!!"); } } \ No newline at end of file