-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotController.java
More file actions
72 lines (49 loc) · 1.51 KB
/
RobotController.java
File metadata and controls
72 lines (49 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
RobotController.java
*/
/* Example class that monitors some piece of hardware and generates
events
For JavaME, we will have to convert the list/array/iterator to
a datastructure provided by that platform */
//package first;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class RobotController implements Runnable {
private List listeners;
private Random aGenerator;
public RobotController()
{
listeners = new ArrayList();
aGenerator = new Random();
}
public synchronized void addEventListener(RobotEventListener l)
{
listeners.add(l);
}
public synchronized void removeEventListener(RobotEventListener l)
{
listeners.remove(l);
}
public synchronized void fireRobotEvent()
{
RobotEvent anEvent = new RobotEvent(this,1+(aGenerator.nextInt(5)));
Iterator anIterator = listeners.iterator();
System.out.println("Controller firing event "+anEvent);
while (anIterator.hasNext()) {
((RobotEventListener)anIterator.next()).eventReceived(anEvent);
}
}
public void run()
{
System.out.println("RobotController starting on thread "+Thread.currentThread().getId());
while (true) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) { }
//System.out.println("Robot controller doing its thing");
fireRobotEvent();
}
}
} /* RobotController.java */