81 lines
2 KiB
Java
81 lines
2 KiB
Java
package usherbrooke.ift630;
|
|
|
|
import java.lang.Long;
|
|
import java.util.ArrayList;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ExecutorService;
|
|
/**
|
|
* Hello world!
|
|
*
|
|
*/
|
|
public class App
|
|
{
|
|
static private int numStops = 10;
|
|
static private int numBusses = 10;
|
|
static private int numStopPerBus = 2;
|
|
static private int numPassengersPerBus = 2;
|
|
static private int numPassengers = 20;
|
|
static private int numPassengersPerStop = 2;
|
|
static private int numThreads = 5;
|
|
static private int timeBetweenStops = 5;
|
|
|
|
public static void main(String[] args) {
|
|
ExecutorService threads = Executors.newFixedThreadPool(numThreads);
|
|
ArrayList<Stop> stops = new ArrayList<Stop>();
|
|
ArrayList<Bus> busses = new ArrayList<Bus>();
|
|
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
|
|
|
|
for (int k = 0; k < numStops; k++) {
|
|
Stop s = new Stop(k, numPassengersPerStop);
|
|
stops.add(s);
|
|
|
|
threads.submit(s);
|
|
}
|
|
|
|
for (int k = 0; k < numPassengers; k++) {
|
|
Stop start, dest;
|
|
|
|
// select start where there is some room left
|
|
do {
|
|
start = stops.get(((int) (Math.random() * numStops)));
|
|
} while (start.getCurrentCapacity() >= start.getMaxCapacity());
|
|
|
|
dest = stops.get((int) (Math.random() * numStops));
|
|
|
|
Passenger p = new Passenger(k, dest, start);
|
|
passengers.add(p);
|
|
start.addPassenger(p);
|
|
}
|
|
|
|
for (int k = 0; k < numBusses; k++) {
|
|
ArrayList<Stop> s_list = new ArrayList<Stop>();
|
|
Stop s;
|
|
while (s_list.size() < numStopPerBus) {
|
|
do {
|
|
s = stops.get((int) (Math.random() * numStops));
|
|
} while (s_list.contains(s));
|
|
s_list.add(s);
|
|
}
|
|
Bus b = new Bus(s_list, k, timeBetweenStops, numPassengersPerBus);
|
|
|
|
b.printDetails();
|
|
|
|
busses.add(b);
|
|
|
|
synchronized (b) {
|
|
b.notify();
|
|
}
|
|
}
|
|
|
|
// start bus thread
|
|
// (here so init logs are clean)
|
|
for (Bus b : busses) {
|
|
threads.submit(b);
|
|
}
|
|
try {
|
|
threads.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
|
|
} catch (InterruptedException e) {
|
|
}
|
|
}
|
|
}
|