001 package com.hammurapi.eventbus.tests.fastfood;
002
003 import java.util.Random;
004
005 import com.hammurapi.eventbus.local.LocalEventBus;
006
007 /**
008 * Kitchen randomly produces main and side dishes with random interval between productions
009 * @author Pavel Vlasov
010 *
011 */
012 public class Kitchen extends Thread {
013
014 private LocalEventBus<Object, ?, ?> bus;
015 private int numberOfDishes;
016 private Random random;
017
018 public Kitchen(int numberOfDishes, LocalEventBus<Object, ?, ?> eventBus) {
019 this.bus = eventBus;
020 this.numberOfDishes = numberOfDishes;
021 this.random = new Random(System.currentTimeMillis()+this.hashCode());
022 }
023
024 @Override
025 public void run() {
026 try {
027 for (int i=0; i<numberOfDishes; ++i) {
028 switch (random.nextInt(4)) {
029 case 0:
030 bus.post(new Hamburger());
031 break;
032 case 1:
033 bus.post(new Cheeseburger());
034 break;
035 case 2:
036 bus.post(new FrenchFries());
037 break;
038 case 3:
039 bus.post(new Coleslaw());
040 break;
041 }
042 Thread.sleep(random.nextInt(50));
043 }
044 } catch (Exception e) {
045 e.printStackTrace();
046 }
047 }
048
049 }