[笔记]-netty-nioeventloopgroup

Netty版本4.1.36.Final-SNAPSHOT,主要是netty-transport这个包。

摸了好久,其实早该写了,但就是懒得码字。


NioEventLoopGroup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class  extends MultithreadEventLoopGroup {

public () {

this(0);
}

public (int nThreads) {
// Executor null
this(nThreads, (Executor) null);
}

public (int nThreads, Executor executor) {
// SelectorProvider
this(nThreads, executor, SelectorProvider.provider());
}

public (int nThreads, Executor executor, final SelectorProvider selectorProvider) {
// 默认的Select策略
this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
}

public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {
// 默认的拒绝处理
super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
}


protected EventLoop newChild(Executor executor, Object... args) throws Exception {
// NioEventLoop的父级是该NioEventLoopGroup
return new NioEventLoop(this, executor, (SelectorProvider) args[0], ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
}

MultithreadEventLoopGroup

1
2
3
4
5
6
7
8
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup {

protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
// 默认CPU核心数*2
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
}

}

MultithreadEventExecutorGroup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup {

/**
* 线程
*/
private final EventExecutor[] children;
private final Set<EventExecutor> readonlyChildren;
private final AtomicInteger terminatedChildren = new AtomicInteger();

/**
* 线程选择器
*/
private final EventExecutorChooserFactory.EventExecutorChooser chooser;

protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
// .. .. 线程选择器 ..
this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
}

protected MultithreadEventExecutorGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory, Object... args) {
if (nThreads <= 0) {
// ...
}
if (executor == null) {
// 默认的执行器,execute就新开线程执行
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) {
// ...
} finally {
if (!success) {
// ...
}
}
}
// 根据线程数量返回选择器
chooser = chooserFactory.newChooser(children);

final FutureListener<Object> terminationListener = new FutureListener<Object>() {

public void operationComplete(Future<Object> future) throws Exception {
// ...
}
};

for (EventExecutor e : children) {
e.terminationFuture().addListener(terminationListener);
}

Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
Collections.addAll(childrenSet, children);
readonlyChildren = Collections.unmodifiableSet(childrenSet);
}

}