/** * Run simulation of people in building using concurrency features of * Java. Creates building and shoppers and starts it running. * @author kim * * 05/12/04 */ public class Simulation { // how long it takes to get to next floor private static final int TRAVEL_TIME = 1000; // how long elevator stays on a floor private static final int FLOOR_TIME = 500; // time shopper is shopping on a floor private static final int BUSY_TIME = 3000; // number of elevators in building private static final int NUM_ELEVATORS = 2; // number of people that each elevator can hold private static final int ELEVATOR_CAPACITY = 2; // number of floors in building private static final int NUM_FLOORS = 3; // post: Report all events of simulations, including length of // simulation. public static void main(String args[]) throws InterruptedException { // Create building Building office = new Building(NUM_FLOORS, NUM_ELEVATORS, ELEVATOR_CAPACITY, FLOOR_TIME, TRAVEL_TIME); // create itineraries for shoppers int[] p1Itinerary = { 1, 2, 0 }; int[] p2Itinerary = { 2, 1, 0 }; // create shoppers Person bill = new Person("bill", p1Itinerary, BUSY_TIME, 0, office); Person bob = new Person("bob", p2Itinerary, BUSY_TIME, 0, office); Person anne = new Person("anne", p1Itinerary, BUSY_TIME, 0, office); Person debbie = new Person("debbie", p2Itinerary, BUSY_TIME, 0, office); // start all threads running office.startElevators(); bill.start(); bob.start(); anne.start(); debbie.start(); // Keep track of when we started long startTime = System.currentTimeMillis(); bill.join(); // Don't continue until all threads complete bob.join(); anne.join(); debbie.join(); office.stopElevators(); // Report time that simulation ran long elapsedTime = System.currentTimeMillis() - startTime; System.out.println("Total simulation time: " + elapsedTime + " ms"); } }