Don't create Selector in a lazy fashion, just create it in the constructor. See #582

This commit is contained in:
Norman Maurer 2012-09-03 21:14:00 +02:00
parent 378ea8e475
commit 64b9e8fb40

View File

@ -75,11 +75,6 @@ abstract class AbstractNioWorker implements Worker {
*/ */
private final Executor executor; private final Executor executor;
/**
* Boolean to indicate if this worker has been started.
*/
private boolean started;
/** /**
* If this worker has been started thread will be a reference to the thread * If this worker has been started thread will be a reference to the thread
* used when starting. i.e. the current thread when the run method is executed. * used when starting. i.e. the current thread when the run method is executed.
@ -91,8 +86,6 @@ abstract class AbstractNioWorker implements Worker {
*/ */
volatile Selector selector; volatile Selector selector;
private boolean isShutdown;
/** /**
* Boolean that controls determines if a blocked Selector.select should * Boolean that controls determines if a blocked Selector.select should
* break out of its selection process. In our case we use a timeone for * break out of its selection process. In our case we use a timeone for
@ -142,6 +135,7 @@ abstract class AbstractNioWorker implements Worker {
public AbstractNioWorker(Executor executor, boolean allowShutdownOnIdle) { public AbstractNioWorker(Executor executor, boolean allowShutdownOnIdle) {
this.executor = executor; this.executor = executor;
this.allowShutdownOnIdle = allowShutdownOnIdle; this.allowShutdownOnIdle = allowShutdownOnIdle;
openSelector();
} }
void register(AbstractNioChannel<?> channel, ChannelFuture future) { void register(AbstractNioChannel<?> channel, ChannelFuture future) {
@ -149,7 +143,6 @@ abstract class AbstractNioWorker implements Worker {
Runnable registerTask = createRegisterTask(channel, future); Runnable registerTask = createRegisterTask(channel, future);
synchronized (startStopLock) { synchronized (startStopLock) {
Selector selector = start();
boolean offered = registerTaskQueue.offer(registerTask); boolean offered = registerTaskQueue.offer(registerTask);
assert offered; assert offered;
@ -213,9 +206,7 @@ abstract class AbstractNioWorker implements Worker {
* *
* @return selector * @return selector
*/ */
private Selector start() { private void openSelector() {
if (!started) {
// Open a selector if this worker didn't start yet.
try { try {
selector = Selector.open(); selector = Selector.open();
} catch (Throwable t) { } catch (Throwable t) {
@ -239,12 +230,7 @@ abstract class AbstractNioWorker implements Worker {
// The method will return to the caller at this point. // The method will return to the caller at this point.
} }
} }
}
assert selector != null && selector.isOpen(); assert selector != null && selector.isOpen();
started = true;
return selector;
} }
@ -371,7 +357,6 @@ abstract class AbstractNioWorker implements Worker {
synchronized (startStopLock) { synchronized (startStopLock) {
if (registerTaskQueue.isEmpty() && selector.keys().isEmpty()) { if (registerTaskQueue.isEmpty() && selector.keys().isEmpty()) {
started = false;
try { try {
selector.close(); selector.close();
} catch (IOException e) { } catch (IOException e) {
@ -380,7 +365,6 @@ abstract class AbstractNioWorker implements Worker {
} finally { } finally {
this.selector = null; this.selector = null;
} }
isShutdown = true;
break; break;
} else { } else {
shutdown = false; shutdown = false;
@ -430,9 +414,9 @@ abstract class AbstractNioWorker implements Worker {
eventQueue.offer(task); eventQueue.offer(task);
synchronized (startStopLock) { synchronized (startStopLock) {
// check if the selector was shutdown already. If so execute all // check if the selector was shutdown already or was not started yet. If so execute all
// submitted tasks in the calling thread // submitted tasks in the calling thread
if (isShutdown) { if (selector == null) {
// execute everything in the event queue as the // execute everything in the event queue as the
for (;;) { for (;;) {
Runnable r = eventQueue.poll(); Runnable r = eventQueue.poll();
@ -442,7 +426,6 @@ abstract class AbstractNioWorker implements Worker {
r.run(); r.run();
} }
} else { } else {
start();
if (wakenUp.compareAndSet(false, true)) { if (wakenUp.compareAndSet(false, true)) {
// wake up the selector to speed things // wake up the selector to speed things
Selector selector = this.selector; Selector selector = this.selector;