1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.nio;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.DefaultSelectStrategyFactory;
20 import io.netty.channel.EventLoop;
21 import io.netty.channel.EventLoopTaskQueueFactory;
22 import io.netty.channel.MultithreadEventLoopGroup;
23 import io.netty.channel.SelectStrategyFactory;
24 import io.netty.channel.SingleThreadEventLoop;
25 import io.netty.util.concurrent.EventExecutor;
26 import io.netty.util.concurrent.EventExecutorChooserFactory;
27 import io.netty.util.concurrent.RejectedExecutionHandler;
28 import io.netty.util.concurrent.RejectedExecutionHandlers;
29
30 import java.nio.channels.Selector;
31 import java.nio.channels.spi.SelectorProvider;
32 import java.util.concurrent.Executor;
33 import java.util.concurrent.ThreadFactory;
34
35
36
37
38 public class NioEventLoopGroup extends MultithreadEventLoopGroup {
39
40
41
42
43
44 public NioEventLoopGroup() {
45 this(0);
46 }
47
48
49
50
51
52 public NioEventLoopGroup(int nThreads) {
53 this(nThreads, (Executor) null);
54 }
55
56
57
58
59
60 public NioEventLoopGroup(ThreadFactory threadFactory) {
61 this(0, threadFactory, SelectorProvider.provider());
62 }
63
64
65
66
67
68 public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
69 this(nThreads, threadFactory, SelectorProvider.provider());
70 }
71
72 public NioEventLoopGroup(int nThreads, Executor executor) {
73 this(nThreads, executor, SelectorProvider.provider());
74 }
75
76
77
78
79
80 public NioEventLoopGroup(
81 int nThreads, ThreadFactory threadFactory, final SelectorProvider selectorProvider) {
82 this(nThreads, threadFactory, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
83 }
84
85 public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,
86 final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {
87 super(nThreads, threadFactory, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
88 }
89
90 public NioEventLoopGroup(
91 int nThreads, Executor executor, final SelectorProvider selectorProvider) {
92 this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
93 }
94
95 public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
96 final SelectStrategyFactory selectStrategyFactory) {
97 super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
98 }
99
100 public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
101 final SelectorProvider selectorProvider,
102 final SelectStrategyFactory selectStrategyFactory) {
103 super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory,
104 RejectedExecutionHandlers.reject());
105 }
106
107 public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
108 final SelectorProvider selectorProvider,
109 final SelectStrategyFactory selectStrategyFactory,
110 final RejectedExecutionHandler rejectedExecutionHandler) {
111 super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory, rejectedExecutionHandler);
112 }
113
114 public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
115 final SelectorProvider selectorProvider,
116 final SelectStrategyFactory selectStrategyFactory,
117 final RejectedExecutionHandler rejectedExecutionHandler,
118 final EventLoopTaskQueueFactory taskQueueFactory) {
119 super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory,
120 rejectedExecutionHandler, taskQueueFactory);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
138 SelectorProvider selectorProvider,
139 SelectStrategyFactory selectStrategyFactory,
140 RejectedExecutionHandler rejectedExecutionHandler,
141 EventLoopTaskQueueFactory taskQueueFactory,
142 EventLoopTaskQueueFactory tailTaskQueueFactory) {
143 super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory,
144 rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
145 }
146
147
148
149
150
151 public void setIoRatio(int ioRatio) {
152 for (EventExecutor e: this) {
153 ((NioEventLoop) e).setIoRatio(ioRatio);
154 }
155 }
156
157
158
159
160
161 public void rebuildSelectors() {
162 for (EventExecutor e: this) {
163 ((NioEventLoop) e).rebuildSelector();
164 }
165 }
166
167 @Override
168 protected EventLoop newChild(Executor executor, Object... args) throws Exception {
169 SelectorProvider selectorProvider = (SelectorProvider) args[0];
170 SelectStrategyFactory selectStrategyFactory = (SelectStrategyFactory) args[1];
171 RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[2];
172 EventLoopTaskQueueFactory taskQueueFactory = null;
173 EventLoopTaskQueueFactory tailTaskQueueFactory = null;
174
175 int argsLength = args.length;
176 if (argsLength > 3) {
177 taskQueueFactory = (EventLoopTaskQueueFactory) args[3];
178 }
179 if (argsLength > 4) {
180 tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[4];
181 }
182 return new NioEventLoop(this, executor, selectorProvider,
183 selectStrategyFactory.newSelectStrategy(),
184 rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
185 }
186 }