From dd474b686a139478ada86b4da32ef86455c7aff1 Mon Sep 17 00:00:00 2001 From: violette Date: Tue, 26 Mar 2024 11:48:47 -0400 Subject: [PATCH] added trips instead of stop list --- src/main/java/usherbrooke/ift630/App.java | 35 +++++++----- src/main/java/usherbrooke/ift630/Bus.java | 54 +++++++------------ .../java/usherbrooke/ift630/Passenger.java | 12 ++--- src/main/java/usherbrooke/ift630/Stop.java | 14 +++-- src/main/java/usherbrooke/ift630/Trip.java | 47 ++++++++++++++++ 5 files changed, 96 insertions(+), 66 deletions(-) create mode 100644 src/main/java/usherbrooke/ift630/Trip.java diff --git a/src/main/java/usherbrooke/ift630/App.java b/src/main/java/usherbrooke/ift630/App.java index ca282c2..2239790 100644 --- a/src/main/java/usherbrooke/ift630/App.java +++ b/src/main/java/usherbrooke/ift630/App.java @@ -9,12 +9,14 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class App { - static private int numBusses = 5; + static private int numBusPerTrip = 1; + static private int numTrips = 1; + static private int numBusses = numTrips * numBusPerTrip; static private int numStopPerBus = 8; static private int numStops = numBusses * numStopPerBus; static private int numPassengersPerBus = 10; - static private int numPassengersPerStop = 3; - static private int numPassengers = numPassengersPerStop * numStops; + static private int numPassengersPerStop = 5; + //static private int numPassengers = numPassengersPerStop * numStops; static private int numThreadsBus = 5; static private int numThreadsStop = 2; static private int timeBetweenStops = 5; @@ -27,9 +29,15 @@ public class App { // themselfes out. ArrayList stops = new ArrayList(); ArrayList busses = new ArrayList(); + ArrayList trips = new ArrayList(); ArrayList passengers = new ArrayList(); BlockingQueue blockingQueue = new LinkedBlockingQueue(); + if (numStopPerBus > numStops) { + System.out.println("More stops per bus than stops."); + return; + } + // make stop" + name + " reached stop " + nextStop.getStopName() + "!"s for (int k = 0; k < numStops; k++) { Stop s = new Stop(k, numPassengersPerStop); @@ -40,16 +48,10 @@ public class App { threadsStop.submit(s); } - // make busses - for (int k = 0; k < numBusses; k++) { - if (numStopPerBus > numStops) { - System.out.println("More stops per bus than stops."); - return; - } - - ArrayList s_list = new ArrayList(); + // make trpis + for (int k = 0 ; k < numTrips ; k++) { Stop s; - // add stop. Make a bus doesnt stop multiple time at the same stop + ArrayList s_list = new ArrayList(); while (s_list.size() < numStopPerBus) { do { s = stops.get((int) (Math.random() * numStops)); @@ -57,7 +59,13 @@ public class App { s_list.add(s); } - Bus b = new Bus(s_list, k, timeBetweenStops, timeEmbark, numPassengersPerBus); + Trip t = new Trip(k, s_list); + trips.add(t); + } + + // make busses + for (int k = 0; k < numBusses; k++) { + Bus b = new Bus(trips.get(k % trips.size()), k, timeBetweenStops, timeEmbark, numPassengersPerBus); b.setBlockingQueue(blockingQueue); busses.add(b); @@ -82,7 +90,6 @@ 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++; diff --git a/src/main/java/usherbrooke/ift630/Bus.java b/src/main/java/usherbrooke/ift630/Bus.java index bc0c4d5..e9256e5 100644 --- a/src/main/java/usherbrooke/ift630/Bus.java +++ b/src/main/java/usherbrooke/ift630/Bus.java @@ -1,12 +1,12 @@ package usherbrooke.ift630; +import java.util.List; import java.util.ArrayList; import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ExecutionException; public class Bus extends Thread { private ArrayList passengers; - private ArrayList stops; + private Trip trip; private String name; private int id; private int maxCapacity; @@ -14,6 +14,7 @@ public class Bus extends Thread { private int timeBetweenStops; private int timeToNextStop; private int timeEmbark; + private int nextStopIndex = 0; private Stop nextStop; private BlockingQueue blockingQueue; @@ -47,7 +48,7 @@ public class Bus extends Thread { // if no reason to stop, skip current stop boolean nextStopEmpty, overMaxCapacity; do { - nextStop = stops.remove(0); + nextStop = trip.get(++nextStopIndex); sendNextStopInfo(); @@ -79,21 +80,13 @@ public class Bus extends Thread { ArrayList res = new ArrayList(); if (nextStop != null) - for (Passenger p : passengers) { - if (p.getDest() == nextStop) { - res.add(p); - } - - } - + res = nextStop.getPassengerByDest(trip, nextStopIndex); return res; } // lock until passenger got in - private void waitEmbark() throws InterruptedException { - synchronized (this) { - sleep(timeEmbark()); - } + private synchronized void waitEmbark() throws InterruptedException { + sleep(timeEmbark()); } // lock until passenger got out @@ -127,14 +120,14 @@ public class Bus extends Thread { Logger.getInstance().print(id, "[BUS] " + name + " exiting!"); } - Bus(ArrayList s, int id, int timeStop, int timeEmbark, int maxCapacity) { + Bus(Trip trajet, int id, int timeStop, int timeEmbark, int maxCapacity) { this.timeBetweenStops = timeStop * 1000 + 1; this.timeEmbark = timeEmbark * 1000 + 1; this.name = "Bus n°" + id; this.id = id; this.passengers = new ArrayList(); - this.stops = s; - this.nextStop = stops.get(0); + this.trip = trajet; + this.nextStop = trajet.get(0); this.currentCapacity = 0; this.maxCapacity = maxCapacity; } @@ -144,10 +137,9 @@ public class Bus extends Thread { // lock on next stop synchronized (nextStop) { for (Passenger p : passengers) { - waitEmbark(); - if (p.getDest() == nextStop) { synchronized (p) { + waitEmbark(); p.notify(); } // already synced on this, func is synced @@ -162,11 +154,8 @@ public class Bus extends Thread { public synchronized void embarkPassengers() throws InterruptedException { synchronized (nextStop) { try { - ArrayList list = nextStop.getPassengerByDest(stops); + ArrayList list = nextStop.getPassengerByDest(trip, nextStopIndex); for (Passenger p : list) { - synchronized (p) { - p.notify(); - } waitEmbark(); if (currentCapacity >= maxCapacity) continue; @@ -189,9 +178,9 @@ 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)) + if (nextStop != trip.get(0)) nextStop.printDetails(id, indent + 1); - for (Stop s : stops) { + for (Stop s : trip.getStops()) { s.printDetails(id, indent + 1); } } @@ -201,24 +190,19 @@ public class Bus extends Thread { printDetails(0); } - // return stop list - public ArrayList getStops() { - ArrayList res = new ArrayList(stops); - if (nextStop != stops.get(0)) - res.add(0, nextStop); - - return res; - } - // return last stop public Stop getTerminus() { - return stops.get(stops.size() - 1); + return trip.getTerminus(); } public String getNameBus() { return name; } + public List getStops() { + return trip.getStops(); + } + public void setBlockingQueue(BlockingQueue q) { blockingQueue = q; } diff --git a/src/main/java/usherbrooke/ift630/Passenger.java b/src/main/java/usherbrooke/ift630/Passenger.java index ec52748..04a80b5 100644 --- a/src/main/java/usherbrooke/ift630/Passenger.java +++ b/src/main/java/usherbrooke/ift630/Passenger.java @@ -10,16 +10,14 @@ public class Passenger extends Thread { private int id; private Stop dest; private Stop start; - private ExecutorService threads; private ExecutorService ex; - private Future 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. + this.ex = Executors.newFixedThreadPool(1); // one for each future. } public void run() { @@ -37,7 +35,7 @@ public class Passenger extends Thread { synchronized (this) { wait(); } - Logger.getInstance().print(id, "\t[PASSENGER] I'm embarking at " + start.getStopName()); + Logger.getInstance().print(id, "\t[PASSENGER] " + name + " embarking at " + start.getStopName()); return null; }); } @@ -48,7 +46,7 @@ public class Passenger extends Thread { synchronized (this) { wait(); } - Logger.getInstance().print(id, "\t[PASSENGER] I'm leaving at " + dest.getStopName()); + Logger.getInstance().print(id, "\t[PASSENGER] " + name + " leaving at " + dest.getStopName()); return null; }); } @@ -84,8 +82,4 @@ public class Passenger extends Thread { public void printDetails() { printDetails(0); } - - public void setThreadPool(ExecutorService threads) { - this.threads = threads; - } } diff --git a/src/main/java/usherbrooke/ift630/Stop.java b/src/main/java/usherbrooke/ift630/Stop.java index 9e817d6..00122d6 100644 --- a/src/main/java/usherbrooke/ift630/Stop.java +++ b/src/main/java/usherbrooke/ift630/Stop.java @@ -1,6 +1,5 @@ package usherbrooke.ift630; -import java.util.concurrent.Executors; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.ArrayList; @@ -75,10 +74,10 @@ public class Stop extends Thread { } // return all passenger that stops at a stop in the list - public ArrayList getPassengerByDest(ArrayList list) { + public ArrayList getPassengerByDest(Trip t, int indexStart) { ArrayList res = new ArrayList(); // for all given stop - for (Stop s : list) { + for (Stop s : t.getStops(indexStart)) { // for all passenger at this stop for (Passenger p : passengers) { // if they stop at this stop, add to res @@ -126,11 +125,10 @@ public class Stop extends Thread { } public synchronized void removePassenger(Passenger p) { - try { - passengers.remove(p); - } catch (Exception e) { - System.out.println("exception" + e.getMessage()); - } + if(passengers.remove(p)) + synchronized (p) { + p.notify(); + } } public void setIndent(int indent) { diff --git a/src/main/java/usherbrooke/ift630/Trip.java b/src/main/java/usherbrooke/ift630/Trip.java new file mode 100644 index 0000000..7ff81e6 --- /dev/null +++ b/src/main/java/usherbrooke/ift630/Trip.java @@ -0,0 +1,47 @@ +package usherbrooke.ift630; + +import java.util.ArrayList; +import java.util.List; + +class Trip { + private List stops; + private String name; + private int id; + + Trip(int id, ArrayList stops) { + this.id = id; + this.name = "Trip " + this.id; + this.stops = stops; + } + + public List getStops(int index) { + return stops.subList(index, stops.size() - 1); + } + + public List getStops() { + return stops; + } + + public String getName() { + return name; + } + + public void printDetails() { + printDetails(0); + } + + public void printDetails(int indent) { + Logger.getInstance().print(id, "\t.".repeat(indent) + "[TRIP] " + name); + for (Stop s : stops) { + s.printDetails(id, indent + 1); + } + } + + public Stop get(int idx) { + return stops.get(idx); + } + + public Stop getTerminus() { + return stops.get(stops.size() - 1); + } +}