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 stops = new ArrayList(); ArrayList busses = new ArrayList(); ArrayList passengers = new ArrayList(); 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 s_list = new ArrayList(); 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) { } } }