added trips instead of stop list
This commit is contained in:
parent
16fe398f5f
commit
dd474b686a
5 changed files with 96 additions and 66 deletions
|
@ -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<Stop> stops = new ArrayList<Stop>();
|
||||
ArrayList<Bus> busses = new ArrayList<Bus>();
|
||||
ArrayList<Trip> trips = new ArrayList<Trip>();
|
||||
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
|
||||
BlockingQueue<BusInformationMessage> blockingQueue = new LinkedBlockingQueue<BusInformationMessage>();
|
||||
|
||||
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<Stop> s_list = new ArrayList<Stop>();
|
||||
// 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<Stop> s_list = new ArrayList<Stop>();
|
||||
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++;
|
||||
|
|
|
@ -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<Passenger> passengers;
|
||||
private ArrayList<Stop> 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<BusInformationMessage> 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,22 +80,14 @@ public class Bus extends Thread {
|
|||
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
||||
|
||||
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) {
|
||||
private synchronized void waitEmbark() throws InterruptedException {
|
||||
sleep(timeEmbark());
|
||||
}
|
||||
}
|
||||
|
||||
// lock until passenger got out
|
||||
private void waitStop() throws InterruptedException {
|
||||
|
@ -127,14 +120,14 @@ public class Bus extends Thread {
|
|||
Logger.getInstance().print(id, "[BUS] " + name + " exiting!");
|
||||
}
|
||||
|
||||
Bus(ArrayList<Stop> 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<Passenger>();
|
||||
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<Passenger> list = nextStop.getPassengerByDest(stops);
|
||||
ArrayList<Passenger> 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<Stop> getStops() {
|
||||
ArrayList<Stop> res = new ArrayList<Stop>(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<Stop> getStops() {
|
||||
return trip.getStops();
|
||||
}
|
||||
|
||||
public void setBlockingQueue(BlockingQueue<BusInformationMessage> q) {
|
||||
blockingQueue = q;
|
||||
}
|
||||
|
|
|
@ -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<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.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Passenger> getPassengerByDest(ArrayList<Stop> list) {
|
||||
public ArrayList<Passenger> getPassengerByDest(Trip t, int indexStart) {
|
||||
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
||||
// 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,10 +125,9 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
47
src/main/java/usherbrooke/ift630/Trip.java
Normal file
47
src/main/java/usherbrooke/ift630/Trip.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
package usherbrooke.ift630;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Trip {
|
||||
private List<Stop> stops;
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
Trip(int id, ArrayList<Stop> stops) {
|
||||
this.id = id;
|
||||
this.name = "Trip " + this.id;
|
||||
this.stops = stops;
|
||||
}
|
||||
|
||||
public List<Stop> getStops(int index) {
|
||||
return stops.subList(index, stops.size() - 1);
|
||||
}
|
||||
|
||||
public List<Stop> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue