Mercurial > hg > Members > shoshi > distributedalgorithm
diff src/main/java/suikwasha/distributedalgorithm/framework/Selector.java @ 1:d24bcb819032
trying to add Selector
author | suikwasha |
---|---|
date | Fri, 19 Oct 2012 23:48:11 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/suikwasha/distributedalgorithm/framework/Selector.java Fri Oct 19 23:48:11 2012 +0900 @@ -0,0 +1,42 @@ +package suikwasha.distributedalgorithm.framework; + +import java.util.LinkedList; +import java.util.concurrent.SynchronousQueue; + +public class Selector +{ + private final SynchronousQueue<Port> queue; + private final LinkedList<Port> watchList; + + public Selector() + { + queue = new SynchronousQueue<Port>(); + watchList = new LinkedList<Port>(); + } + + public void signal(Port _p) + { + queue.offer(_p); + } + + public void register(Port _p) + { + watchList.add(_p); + } + + public Port select() throws InterruptedException + { + Port availablePort; + int length = watchList.size(); + for(int i = 0;i < length;i ++){ + availablePort = watchList.poll(); + if(availablePort.isReady()){ + watchList.addLast(availablePort); + return availablePort; + } + watchList.addLast(availablePort); + } + + return queue.take(); + } +}