This commit is contained in:
violette 2024-03-24 23:36:50 -04:00
parent 3b99a35740
commit 16fe398f5f
4 changed files with 135 additions and 79 deletions

View file

@ -23,12 +23,14 @@ public class App {
public static void main(String[] args) {
ExecutorService threadsBus = Executors.newFixedThreadPool(numThreadsBus);
ExecutorService threadsStop = Executors.newFixedThreadPool(numThreadsStop);
ExecutorService threadsPassenger = Executors.newFixedThreadPool(1); // doesnt need more! future balance
// themselfes out.
ArrayList<Stop> stops = new ArrayList<Stop>();
ArrayList<Bus> busses = new ArrayList<Bus>();
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
BlockingQueue<BusInformationMessage> blockingQueue = new LinkedBlockingQueue<BusInformationMessage>();
// make stops
// make stop" + name + " reached stop " + nextStop.getStopName() + "!"s
for (int k = 0; k < numStops; k++) {
Stop s = new Stop(k, numPassengersPerStop);
s.setBlockingQueue(blockingQueue);
@ -36,14 +38,14 @@ public class App {
stops.add(s);
s.setThreadPool(threadsStop);
threadsStop.submit(s);
//s.run();
}
// make busses
for (int k = 0; k < numBusses; k++) {
if (numStopPerBus > numStops)
{System.out.println("More stops per bus than stops."); return;}
if (numStopPerBus > numStops) {
System.out.println("More stops per bus than stops.");
return;
}
ArrayList<Stop> s_list = new ArrayList<Stop>();
Stop s;
@ -80,9 +82,12 @@ public class App {
dest = b.getStops().get(idx + 1 + (int) Math.round(Math.random() * (numStopPerBus - 2 - idx)));
Passenger p = new Passenger(idPassenger, start, dest);
p.setThreadPool(threadsPassenger);
passengers.add(p);
start.addPassenger(p);
idPassenger++;
threadsPassenger.submit(p);
}
}
}
@ -106,11 +111,15 @@ public class App {
threadsBus.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {}
// block stop pool once bus have finished
// block stop and passenger pool once bus have finished
threadsStop.shutdown();
threadsPassenger.shutdown();
try {
threadsStop.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {}
try {
threadsPassenger.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {}
}
}

View file

@ -2,6 +2,7 @@ package usherbrooke.ift630;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
public class Bus extends Thread {
private ArrayList<Passenger> passengers;
@ -120,7 +121,7 @@ public class Bus extends Thread {
nextStop = null;
}
}
} catch (InterruptedException e) {
} catch (Exception e) {
Logger.getInstance().print(id, "[BUS] exception: " + e.toString());
}
Logger.getInstance().print(id, "[BUS] " + name + " exiting!");
@ -145,11 +146,11 @@ public class Bus extends Thread {
for (Passenger p : passengers) {
waitEmbark();
Logger.getInstance().print(id,
"\t[DISEMBARK] " + p.getName() + " at " + nextStop.getStopName());
if (p.getDest() == nextStop) {
p.disembark();
synchronized (p) {
p.notify();
}
// already synced on this, func is synced
currentCapacity--;
}
}
@ -163,15 +164,15 @@ public class Bus extends Thread {
try {
ArrayList<Passenger> list = nextStop.getPassengerByDest(stops);
for (Passenger p : list) {
synchronized (p) {
p.notify();
}
waitEmbark();
if (currentCapacity >= maxCapacity)
continue;
passengers.add(p);
nextStop.removePassenger(p);
currentCapacity++;
Logger.getInstance().print(id,
"\t[EMBARK] " + p.getName() + " at " + nextStop.getStopName());
}
} catch (IndexOutOfBoundsException e) {
Logger.getInstance().print(id, "Exception: " + e.getMessage());
@ -188,7 +189,6 @@ public class Bus extends Thread {
Logger.getInstance().print(id, "\t".repeat(indent) + "current stop: " + nextStop.getStopName());
Logger.getInstance().print(id, "\t".repeat(indent) + "stops: ");
if (nextStop != stops.get(0))
nextStop.printDetails(id, indent + 1);
for (Stop s : stops) {

View file

@ -1,24 +1,57 @@
package usherbrooke.ift630;
public class Passenger {
import java.util.concurrent.Future;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class Passenger extends Thread {
private String name;
private int id;
private Stop dest;
private Stop start;
private ExecutorService threads;
private ExecutorService ex;
private Future<Void> future;
Passenger(int id, Stop start, Stop dest) {
this.id = id;
this.name = "Passenger " + id;
this.dest = dest;
this.start = start;
this.ex = Executors.newFixedThreadPool(2); // one for each future.
}
// unused, *yet*
public synchronized void embark() {}
public void run() {
try {
embark();
disembark();
} catch (Exception e) {
Logger.getInstance().print(id, "[PASSENGER] " + e.toString());
}
}
// unused, *yet*
public synchronized void disembark() {}
// embark people once thread notified
private synchronized Future<Void> embark() {
return ex.submit(() -> {
synchronized (this) {
wait();
}
Logger.getInstance().print(id, "\t[PASSENGER] I'm embarking at " + start.getStopName());
return null;
});
}
// disembark people once thread notified another time.
private synchronized Future<Void> disembark() {
return ex.submit(() -> {
synchronized (this) {
wait();
}
Logger.getInstance().print(id, "\t[PASSENGER] I'm leaving at " + dest.getStopName());
return null;
});
}
public Stop getStart() {
return start;
@ -28,24 +61,31 @@ public class Passenger {
return dest;
}
public String getName() {
public String getPassengerName() {
return name;
}
// Pretty print, with 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.getStopName() + " dest: " + dest.getStopName());
Logger.getInstance().print(color,
"\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3));
Logger.getInstance().print(color,
"\t".repeat(indent) + name + " start: " + start.getStopName() + " dest: " + dest.getStopName());
}
// Pretty print, with indent!
public void printDetails(int indent) {
Logger.getInstance().print(id, "\t".repeat(indent) + "---".repeat(3) + " Passenger details " + "---".repeat(3));
Logger.getInstance().print(id, "\t".repeat(indent) + name + " start: " + start.getStopName() + " dest: " + dest.getStopName());
Logger.getInstance().print(id,
"\t".repeat(indent) + name + " start: " + start.getStopName() + " dest: " + dest.getStopName());
}
// Pretty print!
public void printDetails() {
printDetails(0);
}
public void setThreadPool(ExecutorService threads) {
this.threads = threads;
}
}

View file

@ -1,8 +1,9 @@
package usherbrooke.ift630;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.ArrayList;
public class Stop extends Thread {
private String name;
@ -13,7 +14,6 @@ public class Stop extends Thread {
private ExecutorService threads;
private BlockingQueue<BusInformationMessage> blockingQueue;
// print bus info times.
private void displayInfos(int indent) throws InterruptedException, ExitException {
BusInformationMessage info = null;
@ -30,7 +30,8 @@ public class Stop extends Thread {
Logger.getInstance().print(id,
"\t".repeat(indent) + "[STOP] " + info.getBus(this).getNameBus() + " arrives in " + time + "s");
// else, catch
} catch (NullPointerException e) {}
} catch (NullPointerException e) {
}
} catch (UnauthorizedException e) {
// if unauth, requeue message.
@ -41,8 +42,13 @@ public class Stop extends Thread {
// loop, and ensure that every stop is reactive
synchronized (this) {
threads.submit(this);
// i dont have access to virtual threads, and futures are for the next question.
// to not hog allof the pool's capacity,
// i have to manualy change threads in action. This is done by submiting this
// thread, waiting a bit (dont blow up our CPU) then exit. This ensure that
// another thread takes the slot, if it there is one.
sleep(10);
threads.submit(this);
throw new ExitException();
}
}
@ -53,7 +59,8 @@ public class Stop extends Thread {
for (;;) {
displayInfos(indent);
}
} catch (InterruptedException | ExitException e) {}
} catch (InterruptedException | ExitException e) {
}
}
Stop(int id, int maxCapacity) {
@ -118,7 +125,7 @@ public class Stop extends Thread {
printDetails(0);
}
public void removePassenger(Passenger p) {
public synchronized void removePassenger(Passenger p) {
try {
passengers.remove(p);
} catch (Exception e) {