# HG changeset patch # User nobuyasu # Date 1310115982 -32400 # Node ID b2604c5c6a2506ecbdba62894fd50c1619fedafc # Parent 7e96da3131ca5135673445085225141d639cfe6c add MulticastQueue.java diff -r 7e96da3131ca -r b2604c5c6a25 src/myVncProxy/MulticastQueue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncProxy/MulticastQueue.java Fri Jul 08 18:06:22 2011 +0900 @@ -0,0 +1,75 @@ +package myVncProxy; + +import java.util.concurrent.CountDownLatch; + +public class MulticastQueue +{ + + Node tail; + + public MulticastQueue() + { + tail = new Node(null); + } + + public synchronized void put(T item) + { + Node next = new Node(item); + tail.set(next); + tail = next; + } + + public Client newClient() + { + return new Client(tail); + } + + static class Client + { + Node node; + + Client(Node tail) + { + node = tail; + } + + public T poll() + { + Node next = null; + + try { + next = node.next(); + }catch(InterruptedException _e){ + _e.printStackTrace(); + } + node = next; + return next.item; + } + } + + private static class Node + { + private T item; + private Node next; + private CountDownLatch latch; + + public Node(T item) + { + this.item = item; + this.next = null; + latch = new CountDownLatch(1); + } + + public void set(Node next) + { + this.next = next; + latch.countDown(); + } + + public Node next() throws InterruptedException + { + latch.await(); + return next; + } + } +}