changeset 56:b2604c5c6a25

add MulticastQueue.java
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Fri, 08 Jul 2011 18:06:22 +0900
parents 7e96da3131ca
children a60ec7414931
files src/myVncProxy/MulticastQueue.java
diffstat 1 files changed, 75 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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<T>
+{
+	
+	Node<T> tail;
+	
+	public MulticastQueue()
+	{
+		tail = new Node<T>(null);
+	}
+	
+	public synchronized void put(T item)
+	{
+		Node<T> next = new Node<T>(item);
+		tail.set(next);
+		tail = next;
+	}
+	
+	public Client<T> newClient()
+	{
+		return new Client<T>(tail);
+	}
+	
+	static class Client<T>
+	{
+		Node<T> node;
+		
+		Client(Node<T> tail)
+		{
+			node = tail;
+		}
+		
+		public T poll()
+		{
+			Node<T> next = null;
+			
+			try {
+				next = node.next();
+			}catch(InterruptedException _e){
+				_e.printStackTrace();
+			}
+			node = next;
+			return next.item;
+		}
+	}
+	
+	private static class Node<T>
+	{
+		private T item;
+		private Node<T> next;
+		private CountDownLatch latch;
+		
+		public Node(T item)
+		{
+			this.item = item;
+			this.next = null;
+			latch = new CountDownLatch(1);
+		}
+		
+		public void set(Node<T> next)
+		{
+			this.next = next;
+			latch.countDown();
+		}
+		
+		public Node<T> next() throws InterruptedException
+		{
+			latch.await();
+			return next;
+		}
+	}
+}