Compare commits
No commits in common. "c7d054e9736bf40984cf5c55138e68160b0ea2cc" and "28ab30e73537585b8f3c1558f925e42df2a386f4" have entirely different histories.
c7d054e973
...
28ab30e735
@ -1,22 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
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";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
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 _) {}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
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.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
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 +0,0 @@
|
|||||||
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