Allow using non-BlockingQueue

- NioEventLoop never uses takeTask(), and ConcurrentLinkedList is faster
  than LinkedBlockingQueue
This commit is contained in:
Trustin Lee 2012-08-18 18:48:44 +09:00
parent 421eabe666
commit f75ba72aeb
2 changed files with 21 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.BlockingQueue;
@ -46,7 +47,7 @@ public abstract class SingleThreadEventExecutor extends AbstractExecutorService
}
private final EventExecutorGroup parent;
private final BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
private final Queue<Runnable> taskQueue;
private final Thread thread;
private final Object stateLock = new Object();
private final Semaphore threadLock = new Semaphore(0);
@ -106,6 +107,12 @@ public abstract class SingleThreadEventExecutor extends AbstractExecutorService
}
}
});
taskQueue = newTaskQueue();
}
protected Queue<Runnable> newTaskQueue() {
return new LinkedBlockingQueue<Runnable>();
}
@Override
@ -129,7 +136,11 @@ public abstract class SingleThreadEventExecutor extends AbstractExecutorService
protected Runnable takeTask() throws InterruptedException {
assert inEventLoop();
return taskQueue.take();
if (taskQueue instanceof BlockingQueue) {
return ((BlockingQueue<Runnable>) taskQueue).take();
} else {
throw new UnsupportedOperationException();
}
}
protected Runnable peekTask() {

View File

@ -31,7 +31,9 @@ import java.nio.channels.spi.SelectorProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
@ -79,6 +81,12 @@ final class NioEventLoop extends SingleThreadEventLoop {
}
}
@Override
protected Queue<Runnable> newTaskQueue() {
// This event loop never calls takeTask()
return new ConcurrentLinkedQueue<Runnable>();
}
@Override
protected void run() {
Selector selector = this.selector;