1.EventExecutorGroup
從命名來看蜈垮,其是EventExecutor的一個容器,next方法就是選擇一個EventExecutor
public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor> {
EventExecutor next();
}
2.EventExecutor
同時EventExecutor也繼承了EventExecutorGroup址晕,這點有些不可理解,既然是一個group,那么其next方法如何實現(xiàn)?
3.next方法實現(xiàn)
3.1 MultithreadEventExecutorGroup
MultithreadEventExecutorGroup實現(xiàn)了next方法,其使用了一個EventExecutorChooser選擇器來選擇EventExecutor
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup {
private final EventExecutorChooserFactory.EventExecutorChooser chooser;
@Override
public EventExecutor next() {
return chooser.next();
}
}
3.2 AbstractEventExecutor
AbstractEventExecutor直接返回的是this,所以其是一個單容器實現(xiàn)
public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
@Override
public EventExecutor next() {
return this;
}
3.3 AbstractEventExecutorGroup
基本可以認為是EventExecutor的代理執(zhí)行,代碼基本類似
public abstract class AbstractEventExecutorGroup implements EventExecutorGroup {
@Override
public Future<?> submit(Runnable task) {
return next().submit(task);
}
}
4.EventExecutorChooser
public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {
public static final DefaultEventExecutorChooserFactory INSTANCE = new DefaultEventExecutorChooserFactory();
private DefaultEventExecutorChooserFactory() { }
@SuppressWarnings("unchecked")
@Override
public EventExecutorChooser newChooser(EventExecutor[] executors) {
if (isPowerOfTwo(executors.length)) {
return new PowerOfTwoEventExecutorChooser(executors);
} else {
return new GenericEventExecutorChooser(executors);
}
}
private static boolean isPowerOfTwo(int val) {
return (val & -val) == val;
}
private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[idx.getAndIncrement() & executors.length - 1];
}
}
private static final class GenericEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}
}
線程池數(shù)量使用2的冪次方逛漫,這樣線程池選擇線程時使用位操作赌莺,能使性能最高震叙。
5.NioEventLoop的初始化
在MultithreadEventExecutorGroup的構(gòu)造函數(shù)中栈雳,通過newChild方法初始化
NioEventLoopGroup重寫了newChild方法怯邪,返回的是NioEventLoop對象
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
if (nThreads <= 0) {
throw new IllegalArgumentException(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if (executor == null) {
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
children = new EventExecutor[nThreads];
for (int i = 0; i < nThreads; i ++) {
boolean success = false;
try {
children[i] = newChild(executor, args);
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
public class NioEventLoopGroup extends MultithreadEventLoopGroup {
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
}
最終的初始化結(jié)果:
以上分析鸠踪,基本可以看到EventExecutorGroup的核心在于選擇EventExecutor,具體的執(zhí)行方法都在EventExecutor中實現(xiàn)