From b3a0d1f7a1d5f3ab38cee40dfa4d03e19fb3265e Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 10 Dec 2024 14:57:33 +0100 Subject: [PATCH] added ex H2 --- src/exercises/ex_h2/Bridge.java | 14 ++++++++++ src/exercises/ex_h2/Road.java | 14 ++++++++++ src/exercises/ex_h2/TestDeadlockBridge.java | 13 +++++++++ src/exercises/ex_h2/Vehicle.java | 26 +++++++++++++++++ .../ex_h2/VehicleWithoutDeadlock.java | 28 +++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 src/exercises/ex_h2/Bridge.java create mode 100644 src/exercises/ex_h2/Road.java create mode 100644 src/exercises/ex_h2/TestDeadlockBridge.java create mode 100644 src/exercises/ex_h2/Vehicle.java create mode 100644 src/exercises/ex_h2/VehicleWithoutDeadlock.java diff --git a/src/exercises/ex_h2/Bridge.java b/src/exercises/ex_h2/Bridge.java new file mode 100644 index 0000000..fa6eaa2 --- /dev/null +++ b/src/exercises/ex_h2/Bridge.java @@ -0,0 +1,14 @@ +package exercises.ex_h2; + +public class Bridge { + private Road roadEast = new Road("East"); + private Road roadWest = new Road("West"); + + public Road getRoadEast() { + return roadEast; + } + + public Road getRoadWest() { + return roadWest; + } +} diff --git a/src/exercises/ex_h2/Road.java b/src/exercises/ex_h2/Road.java new file mode 100644 index 0000000..3846b8f --- /dev/null +++ b/src/exercises/ex_h2/Road.java @@ -0,0 +1,14 @@ +package exercises.ex_h2; + +public class Road { + private String position; + + public Road(String position) { + this.position = position; + } + + @Override + public String toString() { + return position + " road"; + } +} \ No newline at end of file diff --git a/src/exercises/ex_h2/TestDeadlockBridge.java b/src/exercises/ex_h2/TestDeadlockBridge.java new file mode 100644 index 0000000..0fb6e4e --- /dev/null +++ b/src/exercises/ex_h2/TestDeadlockBridge.java @@ -0,0 +1,13 @@ +package exercises.ex_h2; + +public class TestDeadlockBridge { + public static void main(String[] args) { + Bridge bridge = new Bridge(); + //(new Thread(new Vehicle("Ferrari", bridge, bridge.getRoadEast()))).start(); + //(new Thread(new Vehicle("BMW", bridge, bridge.getRoadWest()))).start(); + // Version without DeadLock + (new Thread(new VehicleWithoutDeadlock("Ferrari", bridge, bridge.getRoadEast()))).start(); + (new Thread(new VehicleWithoutDeadlock("BMW", bridge, bridge.getRoadWest()))).start(); + + } +} diff --git a/src/exercises/ex_h2/Vehicle.java b/src/exercises/ex_h2/Vehicle.java new file mode 100644 index 0000000..9536b40 --- /dev/null +++ b/src/exercises/ex_h2/Vehicle.java @@ -0,0 +1,26 @@ +package exercises.ex_h2; + +public class Vehicle implements Runnable { + String name; + private Bridge bridge; + private Road road; + + public Vehicle(String name, Bridge bridge, Road road) { + this.name = name; + this.bridge = bridge; + this.road = road; + } + + @Override + public void run() { + // Do the actions to cross the bridge + synchronized (road) { + System.out.println(name + " is entering on the one-way bridge from " + road); + synchronized (road == bridge.getRoadEast() ? + bridge.getRoadWest(): + bridge.getRoadEast()) { + System.out.println(name + " is leaving the one-way bridge"); + } + } + } +} diff --git a/src/exercises/ex_h2/VehicleWithoutDeadlock.java b/src/exercises/ex_h2/VehicleWithoutDeadlock.java new file mode 100644 index 0000000..3fc05e8 --- /dev/null +++ b/src/exercises/ex_h2/VehicleWithoutDeadlock.java @@ -0,0 +1,28 @@ +package exercises.ex_h2; + +public class VehicleWithoutDeadlock implements Runnable { + String name; + private Bridge bridge; + private Road road; + + public VehicleWithoutDeadlock(String name, Bridge bridge, Road road) { + this.name = name; + this.bridge = bridge; + this.road = road; + } + + @Override + public void run() { + // Do the actions to cross the bridge + synchronized (bridge) { + System.out.println(name + " is entering on the one-way bridge from " + road); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println(name + " is leaving the one-way bridge"); + } + } +} +