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

View file

@ -21,12 +21,11 @@ public class Bus extends Thread {
return (int) (Math.random() * timeBetweenStops);
}
private boolean askStop() {
private boolean stopAsked() {
boolean res = false;
for (Passenger p : passengers){
if (p.getDest() == nextStop) {
res = true;
Logger.getInstance().print(id, "Stop asked");
break;
}
}
@ -36,13 +35,14 @@ public class Bus extends Thread {
private Stop goToNextStop() {
// if no reason to stop, skip current stop
do {
try {
try {
do {
nextStop = stops.remove(0);
} catch (IndexOutOfBoundsException e) {
nextStop = null;
}
} while ((getNextStopPassengers().isEmpty() || currentCapacity >= maxCapacity) && askStop());
} while ((getNextStopPassengers().isEmpty() || currentCapacity >= maxCapacity) && !stopAsked());
} catch (IndexOutOfBoundsException e) {
nextStop = null;
}
try {
Thread.sleep(timeBetweenStops() * 1000);
@ -70,6 +70,7 @@ public class Bus extends Thread {
public void run() {
try {
while (nextStop != null) {
Logger.getInstance().print(id, "[BUS] hop into " + name + " at stop " + nextStop.getName() + "!");
disembarkPassengers();
embarkPassengers();
@ -78,7 +79,7 @@ public class Bus extends Thread {
} catch (Exception e) {
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) {
@ -88,43 +89,47 @@ public class Bus extends Thread {
this.id = id;
this.passengers = new ArrayList<Passenger>();
this.stops = s;
this.nextStop = stops.remove(0);
this.nextStop = stops.remove(0);
this.currentCapacity = 0;
this.maxCapacity = maxCapacity;
}
public synchronized void disembarkPassengers() {
Logger.getInstance().print(id, "[BUS] waiting mutex for " + nextStop.getName());
synchronized (nextStop.getMutex()) {
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) {
p.disembark();
currentCapacity--;
}
}
Logger.getInstance().print(id, "[BUS] release mutex for " + nextStop.getName());
}
}
public synchronized void embarkPassengers() {
Logger.getInstance().print(id, "[BUS] waiting mutex for " + nextStop.getName());
synchronized (nextStop.getMutex()) {
try {
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) {
Thread.sleep(timeEmbark() * 1000);
if (currentCapacity >= maxCapacity)
continue;
passengers.add(p);
nextStop.removePassenger(p);
currentCapacity++;
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, "[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) + "stops: ");
nextStop.printDetails(indent + 1);
nextStop.printDetails(id, indent + 1);
for (Stop s : stops) {
s.printDetails(indent + 1);
s.printDetails(id, indent + 1);
}
}
public void printDetails() {
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 int id;
private int color = -1;
private Stop dest;
private Stop start;
Passenger(int id, Stop dest, Stop start) {
Passenger(int id, Stop start, Stop dest) {
this.id = id;
this.name = "Passenger " + id;
this.dest = dest;
@ -31,14 +30,14 @@ public class Passenger {
return name;
}
public void setName(int color) {
this.color = color;
public void printDetails(int color, int indent) {
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) {
if (color == -1) color = id;
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());
Logger.getInstance().print(id, "\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3));
Logger.getInstance().print(id, "\t".repeat(indent) + name + " start: " + start.getName() + " dest: " + dest.getName());
}
public void printDetails() {

View file

@ -45,9 +45,7 @@ public class Stop implements Runnable {
for (Stop s : list) {
for (Passenger p : passengers) {
if (p.getDest() == s) {
// if we got one, return & abort loop
res.add(p);
break;
}
}
}
@ -71,12 +69,12 @@ public class Stop implements Runnable {
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) + name);
for (Passenger p : passengers) {
p.printDetails(indent + 1);
p.printDetails(color, indent + 1);
}
}