1
final BlockingQueue<Object> blockingQ = new ArrayBlockingQueue<Object>(10);
2
Thread thread = new Thread("consumer thread") {
3
public void run() {
4
for (;;) {
5
try {
6
Object object = blockingQ.poll(1, TimeUnit.SECONDS); //防止死等
7
if (object == null) {
8
continue; // 或者做其他處理
9
}
10
} catch (InterruptedException e) {
11
break;
12
} catch (Exception e) {
13
// handle exception
14
}
15
}
16
}
17
};

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
