1 | package com.hammurapi.eventbus.tests.fastfood; |
2 | |
3 | import java.util.Random; |
4 | |
5 | import com.hammurapi.eventbus.local.LocalEventBus; |
6 | |
7 | /** |
8 | * Kitchen randomly produces main and side dishes with random interval between productions |
9 | * @author Pavel Vlasov |
10 | * |
11 | */ |
12 | public class Kitchen extends Thread { |
13 | |
14 | private LocalEventBus<Object, ?, ?> bus; |
15 | private int numberOfDishes; |
16 | private Random random; |
17 | |
18 | public Kitchen(int numberOfDishes, LocalEventBus<Object, ?, ?> eventBus) { |
19 | this.bus = eventBus; |
20 | this.numberOfDishes = numberOfDishes; |
21 | this.random = new Random(System.currentTimeMillis()+this.hashCode()); |
22 | } |
23 | |
24 | @Override |
25 | public void run() { |
26 | try { |
27 | for (int i=0; i<numberOfDishes; ++i) { |
28 | switch (random.nextInt(4)) { |
29 | case 0: |
30 | bus.post(new Hamburger()); |
31 | break; |
32 | case 1: |
33 | bus.post(new Cheeseburger()); |
34 | break; |
35 | case 2: |
36 | bus.post(new FrenchFries()); |
37 | break; |
38 | case 3: |
39 | bus.post(new Coleslaw()); |
40 | break; |
41 | } |
42 | Thread.sleep(random.nextInt(50)); |
43 | } |
44 | } catch (Exception e) { |
45 | e.printStackTrace(); |
46 | } |
47 | } |
48 | |
49 | } |