Compare commits
4 Commits
28ab30e735
...
c7d054e973
Author | SHA1 | Date | |
---|---|---|---|
c7d054e973 | |||
fe3d1aac35 | |||
e959cdd466 | |||
1cfc40afed |
22
src/exercises/ex_u/FamilyMember.java
Normal file
22
src/exercises/ex_u/FamilyMember.java
Normal file
@ -0,0 +1,22 @@
|
||||
package exercises.ex_u;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class FamilyMember implements Runnable {
|
||||
private final String name;
|
||||
private final Vehicle vehicle;
|
||||
private final CountDownLatch countDownLatch;
|
||||
|
||||
public FamilyMember(String name, Vehicle vehicle, CountDownLatch countDownLatch) {
|
||||
this.name = name;
|
||||
this.vehicle = vehicle;
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
vehicle.addSuitcase(name + "'s suitcase");
|
||||
System.out.println(name + " has loaded their suitcase");
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
}
|
23
src/exercises/ex_u/TestingCountDownLatch_FamilyTrip.java
Normal file
23
src/exercises/ex_u/TestingCountDownLatch_FamilyTrip.java
Normal file
@ -0,0 +1,23 @@
|
||||
package exercises.ex_u;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class TestingCountDownLatch_FamilyTrip {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Vehicle seatAlhambra = new Vehicle();
|
||||
seatAlhambra.printVehicleContent();
|
||||
String[] family = {"Jean", "Anna", "Joseph", "Martha", "Eleonore", "Paul", "Catarina"};
|
||||
// Create a countDownLatch
|
||||
CountDownLatch countDownLatch = new CountDownLatch(family.length);
|
||||
|
||||
// Start the family members
|
||||
// and synchronize them before the start
|
||||
for (int i = 0; i < family.length; i++) {
|
||||
new Thread(new FamilyMember(family[i], seatAlhambra, countDownLatch)).start();
|
||||
}
|
||||
countDownLatch.await();
|
||||
|
||||
seatAlhambra.printVehicleContent();
|
||||
System.out.println("Family trip can start");
|
||||
}
|
||||
}
|
14
src/exercises/ex_u/Vehicle.java
Normal file
14
src/exercises/ex_u/Vehicle.java
Normal file
@ -0,0 +1,14 @@
|
||||
package exercises.ex_u;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class Vehicle {
|
||||
private Queue<String> suitcases = new ConcurrentLinkedQueue<>();
|
||||
public void addSuitcase(String suitcase) {
|
||||
suitcases.add(suitcase);
|
||||
}
|
||||
public void printVehicleContent() {
|
||||
System.out.println("Suitcases: " + suitcases);
|
||||
}
|
||||
}
|
20
src/exercises/ex_v/Referee.java
Normal file
20
src/exercises/ex_v/Referee.java
Normal file
@ -0,0 +1,20 @@
|
||||
package exercises.ex_v;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Referee implements Runnable {
|
||||
private final List<Result> results;
|
||||
|
||||
public Referee(List<Result> 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);
|
||||
}
|
||||
}
|
28
src/exercises/ex_v/Result.java
Normal file
28
src/exercises/ex_v/Result.java
Normal file
@ -0,0 +1,28 @@
|
||||
package exercises.ex_v;
|
||||
|
||||
public class Result implements Comparable<Result>{
|
||||
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";
|
||||
}
|
||||
}
|
34
src/exercises/ex_v/Team.java
Normal file
34
src/exercises/ex_v/Team.java
Normal file
@ -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<Result> results;
|
||||
private final CyclicBarrier barrier;
|
||||
|
||||
public Team(int i, List<Result> 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 _) {}
|
||||
}
|
||||
}
|
22
src/exercises/ex_v/TestingCyclicBarrier_PDG.java
Normal file
22
src/exercises/ex_v/TestingCyclicBarrier_PDG.java
Normal file
@ -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<Result> 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();
|
||||
}
|
||||
}
|
||||
}
|
35
src/exercises/ex_x/FamilyMember.java
Normal file
35
src/exercises/ex_x/FamilyMember.java
Normal file
@ -0,0 +1,35 @@
|
||||
package exercises.ex_x;
|
||||
|
||||
import exercises.Utils;
|
||||
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
public class FamilyMember implements Runnable {
|
||||
private final String name;
|
||||
private final Phaser phaser;
|
||||
private final int numSteps;
|
||||
|
||||
public FamilyMember(String name, Phaser phaser, int numSteps) {
|
||||
this.name = name;
|
||||
this.phaser = phaser;
|
||||
this.numSteps = numSteps;
|
||||
phaser.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int step = 0;
|
||||
while (step < numSteps) {
|
||||
step = phaser.getPhase() + 1;
|
||||
try {
|
||||
System.out.println(name + ": Step " + step + " started");
|
||||
Utils.randomSleep(1000, 3000);
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
phaser.arriveAndDeregister();
|
||||
System.out.println(name + " has arrived");
|
||||
}
|
||||
}
|
24
src/exercises/ex_x/TestingPhaserFamilyBikeTrip.java
Normal file
24
src/exercises/ex_x/TestingPhaserFamilyBikeTrip.java
Normal file
@ -0,0 +1,24 @@
|
||||
package exercises.ex_x;
|
||||
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
public class TestingPhaserFamilyBikeTrip {
|
||||
public static void main(String[] args) {
|
||||
String[] family = {"Father", "Mother", "Son", "Daughter_1", "Daughter_2"};
|
||||
String[] routes = {"Sierre", "Sion", "Martigny", "St-Maurice", "Aigle", "Vevey"};
|
||||
Phaser phaser = new Phaser();
|
||||
phaser.register();
|
||||
|
||||
System.out.println("The family leaves Brig for a bike trip to Vevey");
|
||||
for (int i = 0; i < family.length; i++) {
|
||||
new Thread(new FamilyMember(family[i], phaser, routes.length)).start();
|
||||
}
|
||||
|
||||
for (int i = 0; i < routes.length; i++) {
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
System.out.println("All family members arrived to " + routes[i]);
|
||||
}
|
||||
phaser.arriveAndDeregister();
|
||||
System.out.println("End of the bike trip.");
|
||||
}
|
||||
}
|
21
src/exercises/ex_y/RaceTester.java
Normal file
21
src/exercises/ex_y/RaceTester.java
Normal file
@ -0,0 +1,21 @@
|
||||
package exercises.ex_y;
|
||||
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
public class RaceTester {
|
||||
public static final int NUM_RUNNERS = 10;
|
||||
public static final int NUM_STAGES = 5;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Phaser phaser = new Phaser();
|
||||
phaser.register();
|
||||
for (int i = 0; i < NUM_RUNNERS; i++) {
|
||||
new Thread(new Runner(NUM_STAGES, phaser)).start();
|
||||
}
|
||||
for (int i = 0; i <= NUM_STAGES; i++) {
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
}
|
||||
System.out.println("Finished race");
|
||||
phaser.arriveAndDeregister();
|
||||
}
|
||||
}
|
43
src/exercises/ex_y/Runner.java
Normal file
43
src/exercises/ex_y/Runner.java
Normal file
@ -0,0 +1,43 @@
|
||||
package exercises.ex_y;
|
||||
|
||||
import exercises.Utils;
|
||||
|
||||
import java.util.concurrent.Phaser;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Runner implements Runnable {
|
||||
private static AtomicInteger nextId = new AtomicInteger(0);
|
||||
private final int id;
|
||||
private final int maxStep;
|
||||
private final Phaser phaser;
|
||||
|
||||
public Runner(int maxStep, Phaser phaser) {
|
||||
id = nextId.getAndIncrement();
|
||||
this.maxStep = maxStep;
|
||||
this.phaser = phaser;
|
||||
phaser.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(this + " is on the starting line");
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
|
||||
while (phaser.getPhase() <= this.maxStep) {
|
||||
System.out.println(this + " started stage " + phaser.getPhase());
|
||||
try {
|
||||
Utils.randomSleep(1000, 5000);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
System.out.println(this + " finished stage " + phaser.getPhase());
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
}
|
||||
phaser.arriveAndDeregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Runner " + id;
|
||||
}
|
||||
}
|
1
src/exercises/ex_y/answers.txt
Normal file
1
src/exercises/ex_y/answers.txt
Normal file
@ -0,0 +1 @@
|
||||
We should use a Phaser as it allows for threads to wait on each other before moving to the next step
|
Loading…
x
Reference in New Issue
Block a user