Mercurial > hg > Members > riono > TreeVNC_ja_comment
comparison src/main/java/jp/ac/u_ryukyu/treevnc/MulticastQueue.java @ 12:12c3a73be47f
rename package
author | one |
---|---|
date | Tue, 21 Aug 2012 14:24:38 +0900 |
parents | src/main/java/ac/ryukyu/treevnc/MulticastQueue.java@b32668b8e83c |
children | f1938dd3b518 |
comparison
equal
deleted
inserted
replaced
11:57ae9fbb1245 | 12:12c3a73be47f |
---|---|
1 package jp.ac.u_ryukyu.treevnc; | |
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 | |
27 public static class Client<T> | |
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 } |