Mercurial > hg > Applications > TightVNC_orginal
diff src/main/java/ac/ryukyu/treevnc/MulticastQueue.java @ 3:e7ce2b2ffed8
add and modify files
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 24 Jul 2012 15:46:36 +0900 |
parents | |
children | b32668b8e83c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ac/ryukyu/treevnc/MulticastQueue.java Tue Jul 24 15:46:36 2012 +0900 @@ -0,0 +1,87 @@ +package ac.ryukyu.treevnc; + +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; + } + + synchronized public T poll() + { + Node<T> next = null; + T item = null; + do { + try { + next = node.next(); + }catch(InterruptedException _e){ + continue; + } +// item = node.getItem(); + item = next.getItem(); + node = next; + } while ( item == null); + return item; + } + } + + 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); + } + + synchronized public T getItem() { + return item; + } + + public void set(Node<T> next) + { + this.next = next; + latch.countDown(); + } + + public Node<T> next() throws InterruptedException + { + latch.await(); + return next; + } + + synchronized public void clear() { + item = null; + } + } +}