-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotSystemBQ.java
70 lines (51 loc) · 1.77 KB
/
RobotSystemBQ.java
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
/*
RobotSystemBQ.java
*/
/* robot subsystem that uses blocking queue for queing of events to
handle
*/
//package first;
//import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
public class RobotSystemBQ extends RobotSystem
implements Runnable,RobotEventListener
{
BlockingQueue bQ;
public RobotSystemBQ(int anInt, RobotController c)
{
super(anInt,c);
// allocate blocking queue and initialize
//bQ = new ArrayBlockingQueue(20);
bQ = new BlockingQueue(20);
}
public void eventReceived(RobotEvent e)
{
//System.out.println("RobotSystemBQ "+myNumber+" received event "+e);
// queue up the event in blocking queue
try {
bQ.put((Object)e);
}
catch (InterruptedException iE) { }
catch (NullPointerException npE) { }
}
public void run()
{
RobotEvent anEvent;
System.out.println("RobotSystemBQ "+myNumber+" starting on thread "+Thread.currentThread().getId());
// register to receive events
aController.addEventListener(this);
while (true) {
// wait on an event and then remove from queue
try {
anEvent = (RobotEvent)bQ.take();
}
catch (InterruptedException iE) { continue; }
System.out.println("RobotSystemMQ "+myNumber+
" retreived event "+anEvent+
" on thread "+Thread.currentThread().getId()+
" off of the blocking queue, Q size is now "+
bQ.size());
try { Thread.sleep(500); } catch (InterruptedException iE) { }
}
}
} /* RobotSystemBQ */