@@ -0,0 +1,1291 @@
+// Copyright 2014, Kevin Ko %3Ckevin@faveset.com%3E%0A%0Apackage com.faveset.khttp;%0A%0Aimport java.nio.channels.Selector;%0A%0Aimport java.util.concurrent.BlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingQueue;%0Aimport java.util.concurrent.Executor;%0A%0A/**%0A * A SelectTaskQueue is meant to be used with a Selector.  Submitted%0A * tasks will wake up the associated Selector for immediate execution.%0A * Because of this, all tasks should be short and non-blocking.%0A *%0A * This is intended for waking up (i.e., rescheduling select operations)%0A * a sleeping connection.%0A *%0A * All tasks are placed in an unbounded queue.  Thus, it is best to schedule%0A * just O(1) tasks per connection.%0A */%0Aclass SelectTaskQueue implements Executor %7B%0A    private Selector mSelector;%0A%0A    private BlockingQueue%3CRunnable%3E mTaskQueue;%0A%0A    public SelectTaskQueue(Selector selector) %7B%0A        mSelector = selector;%0A        // We want an unbounded queue, since we never want a task to block.%0A        mTaskQueue = new LinkedBlockingQueue%3CRunnable%3E();%0A    %7D%0A%0A    public void execute(final Runnable r) %7B%0A        mTaskQueue.offer(r);%0A%0A        mSelector.wakeup();%0A    %7D%0A%0A    /**%0A     * @return the head of the queue or null if empty.  This is thread-safe.%0A     */%0A    public Runnable poll() %7B%0A        return mTaskQueue.poll();%0A    %7D%0A%7D%0A

Add SelectTaskQueue for scheduling tasks in a select loop.