Selector selector;
if (firstChannel) {
try {
this.selector = selector = Selector.open();
} catch (IOException e) {
throw new ChannelException(
"Failed to create a selector.", e);
}
} else {
selector = this.selector;
if (selector == null) {
do {
Thread.yield();
selector = this.selector;
} while (selector == null);
}
}
if (firstChannel) {
try {
channel.socket.register(selector, SelectionKey.OP_CONNECT, channel);
} catch (ClosedChannelException e) {
throw new ChannelException(
"Failed to register a socket to the selector.", e);
}
bossExecutor.execute(new ThreadRenamingRunnable(
this, "New I/O client boss #" + id));
} else {
synchronized (selectorGuard) {
selector.wakeup();
try {
channel.socket.register(selector, SelectionKey.OP_CONNECT, channel);
} catch (ClosedChannelException e) {
throw new ChannelException(
"Failed to register a socket to the selector.", e);
}
}
}
}