OOP-Projects-Java

Log | Files | Refs | README

Main.java (1030B)


      1 package breakRoom;
      2 
      3 import java.util.ArrayList;
      4 
      5 public class Main extends Thread {
      6 	
      7 	/**
      8 	 * Used to start the coffee simulation
      9 	 * @throws InterruptedException
     10 	 */
     11 	private void startCoffeSimulation() throws InterruptedException {
     12 		CoffeeQueue queue = new CoffeeQueue();
     13 		CoffeeMaker coffeeMachine = new CoffeeMaker(queue);
     14 		ArrayList<Worker> workers = new ArrayList<Worker>();
     15 		final int numberOfWorkers = 10;
     16 		
     17 		System.out.println("======Started Simulation=====");
     18 		
     19 		//start all workers
     20 		for (int i = 0; i < numberOfWorkers; i++) {
     21 			workers.add(new Worker(("worker" + i), queue));
     22 			workers.get(i).start();
     23 		}
     24 		
     25 		// wait for 20 seconds
     26 		Thread.sleep(20000);
     27 		
     28 		//Stop all workers
     29 		for (Worker worker : workers) {
     30 			worker.cancel();
     31 		}
     32 		
     33 		coffeeMachine.cancel();
     34 
     35 		System.out.println("======Closed Simulation=====");
     36 	}
     37 
     38 	public static void main(String[] args) {
     39 		Main main = new Main();
     40 		try {
     41 			main.startCoffeSimulation();
     42 		} catch (InterruptedException e) {
     43 			e.printStackTrace();
     44 		}
     45 	}
     46 }