diff --git a/src/exercises/ex_v/Referee.java b/src/exercises/ex_v/Referee.java new file mode 100644 index 0000000..9d86aef --- /dev/null +++ b/src/exercises/ex_v/Referee.java @@ -0,0 +1,20 @@ +package exercises.ex_v; + +import java.util.Comparator; +import java.util.List; + +public class Referee implements Runnable { + private final List results; + + public Referee(List results) { + this.results = results; + } + + @Override + public void run() { + System.out.println("Referee starts to establish ranking..."); + results.sort(Result::compareTo); + System.out.println("PDG Team Ranking:"); + results.forEach(System.out::println); + } +} diff --git a/src/exercises/ex_v/Result.java b/src/exercises/ex_v/Result.java new file mode 100644 index 0000000..bdac3f1 --- /dev/null +++ b/src/exercises/ex_v/Result.java @@ -0,0 +1,28 @@ +package exercises.ex_v; + +public class Result implements Comparable{ + private final int time; + private final String team; + public Result(String team, int time) { + this.time = time; + this.team = team; + } + + public int getTime() { + return time; + } + + public String getTeam() { + return team; + } + + @Override + public int compareTo(Result result) { + return this.getTime() - result.getTime(); + } + + @Override + public String toString() { + return getTeam() + " : " + getTime() + "h"; + } +} diff --git a/src/exercises/ex_v/Team.java b/src/exercises/ex_v/Team.java new file mode 100644 index 0000000..9e38517 --- /dev/null +++ b/src/exercises/ex_v/Team.java @@ -0,0 +1,34 @@ +package exercises.ex_v; + +import exercises.Utils; + +import java.util.List; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class Team implements Runnable { + private final String name; + private final List results; + private final CyclicBarrier barrier; + + public Team(int i, List results, CyclicBarrier barrier) { + this.name = "Team " + i; + this.results = results; + this.barrier = barrier; + } + + @Override + public void run() { + System.out.println(name + " : Started the race"); + try { + Utils.randomSleep(1000, 5000); + } catch (InterruptedException _) {} + + Result result = new Result(name, (int) (Math.random() * 20 + 1)); + System.out.println(name + " : Finished the race in " + result.getTime() + " hours and is waiting for other teams"); + results.add(result); + try { + barrier.await(); + } catch (InterruptedException | BrokenBarrierException _) {} + } +} diff --git a/src/exercises/ex_v/TestingCyclicBarrier_PDG.java b/src/exercises/ex_v/TestingCyclicBarrier_PDG.java new file mode 100644 index 0000000..6cf1eb3 --- /dev/null +++ b/src/exercises/ex_v/TestingCyclicBarrier_PDG.java @@ -0,0 +1,22 @@ +package exercises.ex_v; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class TestingCyclicBarrier_PDG { + private static int NUM_TEAMS = 20; + public static void main(String[] args) { + List results = Collections.synchronizedList(new ArrayList<>()); + System.out.println("Starting PDG with " + NUM_TEAMS + " teams "); + CyclicBarrier barrier = new CyclicBarrier(NUM_TEAMS, new Referee(results)); + + // Start the team threads and synchronize them. + // Make sure finally to order to order the results and print them + for (int i = 0; i < NUM_TEAMS; i++) { + new Thread(new Team(i, results, barrier)).start(); + } + } +}