This commit is contained in:
violette 2024-03-22 11:16:14 -04:00
parent 78c88d9279
commit 97ce72dd93
4 changed files with 69 additions and 46 deletions

View file

@ -11,12 +11,12 @@ import java.util.concurrent.ExecutorService;
*/ */
public class App { public class App {
static private int numStops = 10; static private int numStops = 10;
static private int numBusses = 10; static private int numBusses = 5;
static private int numStopPerBus = 2; static private int numStopPerBus = 2;
static private int numPassengersPerBus = 2; static private int numPassengersPerBus = 2;
static private int numPassengersPerStop = 5; static private int numPassengersPerStop = 5;
static private int numPassengers = numPassengersPerStop * numStops; static private int numPassengers = numPassengersPerStop * numStops;
static private int numThreads = 1; static private int numThreads = 5;
static private int timeBetweenStops = 0; static private int timeBetweenStops = 0;
static private int timeEmbark = 0; static private int timeEmbark = 0;
@ -47,21 +47,27 @@ public class App {
busses.add(b); busses.add(b);
} }
// TODO put pasengers at stops they can catch their bus int idPassenger = 0;
// ie, start and stop in bus path + stop after start for (Bus b : busses) {
for (int k = 0; k < numPassengers; k++) { for (Stop start : b.getStops()) {
Stop start, dest; if (start == b.getTerminus())
continue;
// select start where there is some room left int idx = b.getStops().indexOf(start);
do { if (idx == -1 || start == b.getTerminus())
start = stops.get(((int) (Math.random() * numStops))); continue;
} while (start.getCurrentCapacity() >= start.getMaxCapacity());
dest = stops.get((int) (Math.random() * numStops)); for (int k = 0; k < (int) (Math.random() * numPassengersPerStop); k++) {
Stop dest = null;
Passenger p = new Passenger(k, dest, start); dest = b.getStops().get(idx + 1 + (int) Math.round(Math.random() * (numStopPerBus - 2 - idx)));
passengers.add(p);
start.addPassenger(p); Passenger p = new Passenger(idPassenger, start, dest);
passengers.add(p);
start.addPassenger(p);
idPassenger++;
}
}
} }
for (Bus b : busses) { for (Bus b : busses) {
@ -72,8 +78,8 @@ public class App {
// start bus thread // start bus thread
// (here so init logs are clean) // (here so init logs are clean)
for (Bus b : busses) { for (Bus b : busses) {
//threads.submit(b); threads.submit(b);
b.run(); //b.run();
} }
threads.shutdown(); threads.shutdown();

View file

@ -21,12 +21,11 @@ public class Bus extends Thread {
return (int) (Math.random() * timeBetweenStops); return (int) (Math.random() * timeBetweenStops);
} }
private boolean askStop() { private boolean stopAsked() {
boolean res = false; boolean res = false;
for (Passenger p : passengers){ for (Passenger p : passengers){
if (p.getDest() == nextStop) { if (p.getDest() == nextStop) {
res = true; res = true;
Logger.getInstance().print(id, "Stop asked");
break; break;
} }
} }
@ -36,13 +35,14 @@ public class Bus extends Thread {
private Stop goToNextStop() { private Stop goToNextStop() {
// if no reason to stop, skip current stop // if no reason to stop, skip current stop
do { try {
try { do {
nextStop = stops.remove(0); nextStop = stops.remove(0);
} catch (IndexOutOfBoundsException e) { } while ((getNextStopPassengers().isEmpty() || currentCapacity >= maxCapacity) && !stopAsked());
nextStop = null;
} } catch (IndexOutOfBoundsException e) {
} while ((getNextStopPassengers().isEmpty() || currentCapacity >= maxCapacity) && askStop()); nextStop = null;
}
try { try {
Thread.sleep(timeBetweenStops() * 1000); Thread.sleep(timeBetweenStops() * 1000);
@ -70,6 +70,7 @@ public class Bus extends Thread {
public void run() { public void run() {
try { try {
while (nextStop != null) { while (nextStop != null) {
Logger.getInstance().print(id, "[BUS] hop into " + name + " at stop " + nextStop.getName() + "!");
disembarkPassengers(); disembarkPassengers();
embarkPassengers(); embarkPassengers();
@ -78,7 +79,7 @@ public class Bus extends Thread {
} catch (Exception e) { } catch (Exception e) {
Logger.getInstance().print(id, "[BUS] exception: " + e.getMessage()); Logger.getInstance().print(id, "[BUS] exception: " + e.getMessage());
} }
Logger.getInstance().print(id, "[BUS] exiting!"); Logger.getInstance().print(id, "[BUS]" + name + " exiting!");
} }
Bus(ArrayList<Stop> s, int id, int timeStop, int timeEmbark, int maxCapacity) { Bus(ArrayList<Stop> s, int id, int timeStop, int timeEmbark, int maxCapacity) {
@ -88,43 +89,47 @@ public class Bus extends Thread {
this.id = id; this.id = id;
this.passengers = new ArrayList<Passenger>(); this.passengers = new ArrayList<Passenger>();
this.stops = s; this.stops = s;
this.nextStop = stops.remove(0); this.nextStop = stops.remove(0);
this.currentCapacity = 0; this.currentCapacity = 0;
this.maxCapacity = maxCapacity; this.maxCapacity = maxCapacity;
} }
public synchronized void disembarkPassengers() { public synchronized void disembarkPassengers() {
Logger.getInstance().print(id, "[BUS] waiting mutex for " + nextStop.getName());
synchronized (nextStop.getMutex()) { synchronized (nextStop.getMutex()) {
for (Passenger p : passengers) { for (Passenger p : passengers) {
Logger.getInstance().print(id, "\t[BUS] " + p.getName() + " disembarked " + name); Logger.getInstance().print(id,
"\t[DISEMBARK] " + p.getName() + " at " + nextStop.getName());
if (p.getDest() == nextStop) { if (p.getDest() == nextStop) {
p.disembark(); p.disembark();
currentCapacity--; currentCapacity--;
} }
} }
Logger.getInstance().print(id, "[BUS] release mutex for " + nextStop.getName());
} }
} }
public synchronized void embarkPassengers() { public synchronized void embarkPassengers() {
Logger.getInstance().print(id, "[BUS] waiting mutex for " + nextStop.getName());
synchronized (nextStop.getMutex()) { synchronized (nextStop.getMutex()) {
try { try {
ArrayList<Passenger> list = nextStop.getPassengerByDest(stops); ArrayList<Passenger> list = nextStop.getPassengerByDest(stops);
Logger.getInstance().print(id, "[BUS] hop into " + name + " at stop " + nextStop.getName() + "!");
Thread.sleep(timeEmbark() * 1000);
if (currentCapacity >= maxCapacity)
throw new OverCapacityException("Over bus capacity.");
for (Passenger p : list) { for (Passenger p : list) {
Thread.sleep(timeEmbark() * 1000);
if (currentCapacity >= maxCapacity)
continue;
passengers.add(p); passengers.add(p);
nextStop.removePassenger(p); nextStop.removePassenger(p);
currentCapacity++; currentCapacity++;
Logger.getInstance().print(id, Logger.getInstance().print(id,
"\t[BUS] " + p.getName() + " embarked in " + name + " at " + nextStop.getName()); "\t[EMBARK] " + p.getName() + " at " + nextStop.getName());
} }
} catch (IndexOutOfBoundsException | OverCapacityException | InterruptedException e) { } catch (IndexOutOfBoundsException | InterruptedException e) {
Logger.getInstance().print(id, "Exception: " + e.getMessage()); Logger.getInstance().print(id, "Exception: " + e.getMessage());
} }
Logger.getInstance().print(id, "[BUS] release mutex for " + nextStop.getName());
} }
} }
@ -136,13 +141,28 @@ public class Bus extends Thread {
Logger.getInstance().print(id, "\t".repeat(indent) + "current stop: " + nextStop.getName()); Logger.getInstance().print(id, "\t".repeat(indent) + "current stop: " + nextStop.getName());
Logger.getInstance().print(id, "\t".repeat(indent) + "stops: "); Logger.getInstance().print(id, "\t".repeat(indent) + "stops: ");
nextStop.printDetails(indent + 1); nextStop.printDetails(id, indent + 1);
for (Stop s : stops) { for (Stop s : stops) {
s.printDetails(indent + 1); s.printDetails(id, indent + 1);
} }
} }
public void printDetails() { public void printDetails() {
printDetails(0); printDetails(0);
} }
public ArrayList<Stop> getStops() {
ArrayList<Stop> res = new ArrayList<Stop>(stops);
res.add(0, nextStop);
return res;
}
public Stop getTerminus() {
return stops.get(stops.size() - 1);
}
public String getNameBus() {
return name;
}
} }

View file

@ -4,11 +4,10 @@ public class Passenger {
private String name; private String name;
private int id; private int id;
private int color = -1;
private Stop dest; private Stop dest;
private Stop start; private Stop start;
Passenger(int id, Stop dest, Stop start) { Passenger(int id, Stop start, Stop dest) {
this.id = id; this.id = id;
this.name = "Passenger " + id; this.name = "Passenger " + id;
this.dest = dest; this.dest = dest;
@ -31,14 +30,14 @@ public class Passenger {
return name; return name;
} }
public void setName(int color) { public void printDetails(int color, int indent) {
this.color = color; Logger.getInstance().print(color, "\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3));
Logger.getInstance().print(color, "\t".repeat(indent) + name + " start: " + start.getName() + " dest: " + dest.getName());
} }
public void printDetails(int indent) { public void printDetails(int indent) {
if (color == -1) color = id; Logger.getInstance().print(id, "\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3));
Logger.getInstance().print(color, "\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3)); Logger.getInstance().print(id, "\t".repeat(indent) + name + " start: " + start.getName() + " dest: " + dest.getName());
Logger.getInstance().print(color, "\t".repeat(indent) + name + " start: " + start.getName() + " dest: " + dest.getName());
} }
public void printDetails() { public void printDetails() {

View file

@ -45,9 +45,7 @@ public class Stop implements Runnable {
for (Stop s : list) { for (Stop s : list) {
for (Passenger p : passengers) { for (Passenger p : passengers) {
if (p.getDest() == s) { if (p.getDest() == s) {
// if we got one, return & abort loop
res.add(p); res.add(p);
break;
} }
} }
} }
@ -71,12 +69,12 @@ public class Stop implements Runnable {
return mutex; return mutex;
} }
public void printDetails(int indent, int color) { public void printDetails(int color, int indent) {
Logger.getInstance().print(color, "\t".repeat(indent) + "---".repeat(3) + " Stop details " + "---".repeat(3)); Logger.getInstance().print(color, "\t".repeat(indent) + "---".repeat(3) + " Stop details " + "---".repeat(3));
Logger.getInstance().print(color, "\t".repeat(indent) + name); Logger.getInstance().print(color, "\t".repeat(indent) + name);
for (Passenger p : passengers) { for (Passenger p : passengers) {
p.printDetails(indent + 1); p.printDetails(color, indent + 1);
} }
} }