12
|
1 package jp.ac.u_ryukyu.treevnc;
|
3
|
2
|
|
3 import java.util.concurrent.CountDownLatch;
|
|
4
|
|
5 public class MulticastQueue<T>
|
|
6 {
|
|
7
|
|
8 Node<T> tail;
|
|
9
|
|
10 public MulticastQueue()
|
|
11 {
|
|
12 tail = new Node<T>(null);
|
|
13 }
|
|
14
|
|
15 public synchronized void put(T item)
|
|
16 {
|
|
17 Node<T> next = new Node<T>(item);
|
|
18 tail.set(next);
|
|
19 tail = next;
|
|
20 }
|
|
21
|
|
22 public Client<T> newClient()
|
|
23 {
|
|
24 return new Client<T>(tail);
|
|
25 }
|
|
26
|
4
|
27 public static class Client<T>
|
3
|
28 {
|
|
29 Node<T> node;
|
|
30
|
|
31 Client(Node<T> tail)
|
|
32 {
|
|
33 node = tail;
|
|
34 }
|
|
35
|
|
36 synchronized public T poll()
|
|
37 {
|
|
38 Node<T> next = null;
|
|
39 T item = null;
|
|
40 do {
|
|
41 try {
|
|
42 next = node.next();
|
|
43 }catch(InterruptedException _e){
|
|
44 continue;
|
|
45 }
|
|
46 // item = node.getItem();
|
|
47 item = next.getItem();
|
|
48 node = next;
|
|
49 } while ( item == null);
|
|
50 return item;
|
|
51 }
|
|
52 }
|
|
53
|
|
54 static class Node<T>
|
|
55 {
|
|
56 private T item;
|
|
57 private Node<T> next;
|
|
58 private CountDownLatch latch;
|
|
59
|
|
60 public Node(T item)
|
|
61 {
|
|
62 this.item = item;
|
|
63 this.next = null;
|
|
64 latch = new CountDownLatch(1);
|
|
65 }
|
|
66
|
|
67 synchronized public T getItem() {
|
|
68 return item;
|
|
69 }
|
|
70
|
|
71 public void set(Node<T> next)
|
|
72 {
|
|
73 this.next = next;
|
|
74 latch.countDown();
|
|
75 }
|
|
76
|
|
77 public Node<T> next() throws InterruptedException
|
|
78 {
|
|
79 latch.await();
|
|
80 return next;
|
|
81 }
|
|
82
|
|
83 synchronized public void clear() {
|
|
84 item = null;
|
|
85 }
|
|
86 }
|
|
87 }
|