1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
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.MultithreadEventLoopGroup;
21 import io.netty.channel.SelectStrategyFactory;
22 import io.netty.util.concurrent.EventExecutor;
23 import io.netty.util.concurrent.RejectedExecutionHandler;
24 import io.netty.util.concurrent.RejectedExecutionHandlers;
25
26 import java.nio.channels.Selector;
27 import java.nio.channels.spi.SelectorProvider;
28 import java.util.concurrent.ThreadFactory;
29
30 /**
31 * {@link MultithreadEventLoopGroup} implementations which is used for NIO {@link Selector} based {@link Channel}s.
32 */
33 public class NioEventLoopGroup extends MultithreadEventLoopGroup {
34
35 /**
36 * Create a new instance using the default number of threads, the default {@link ThreadFactory} and
37 * the {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
38 */
39 public NioEventLoopGroup() {
40 this(0);
41 }
42
43 /**
44 * Create a new instance using the specified number of threads, {@link ThreadFactory} and the
45 * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
46 */
47 public NioEventLoopGroup(int nThreads) {
48 this(nThreads, null);
49 }
50
51 /**
52 * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the
53 * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
54 */
55 public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
56 this(nThreads, threadFactory, SelectorProvider.provider());
57 }
58
59 /**
60 * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
61 * {@link SelectorProvider}.
62 */
63 public NioEventLoopGroup(
64 int nThreads, ThreadFactory threadFactory, final SelectorProvider selectorProvider) {
65 this(nThreads, threadFactory, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
66 }
67
68 public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,
69 final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {
70 super(nThreads, threadFactory, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
71 }
72
73 public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,
74 final SelectorProvider selectorProvider,
75 final SelectStrategyFactory selectStrategyFactory,
76 final RejectedExecutionHandler rejectedExecutionHandler) {
77 super(nThreads, threadFactory, selectorProvider, selectStrategyFactory, rejectedExecutionHandler);
78 }
79
80 /**
81 * Sets the percentage of the desired amount of time spent for I/O in the child event loops. The default value is
82 * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks.
83 */
84 public void setIoRatio(int ioRatio) {
85 for (EventExecutor e: children()) {
86 ((NioEventLoop) e).setIoRatio(ioRatio);
87 }
88 }
89
90 /**
91 * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work
92 * around the infamous epoll 100% CPU bug.
93 */
94 public void rebuildSelectors() {
95 for (EventExecutor e: children()) {
96 ((NioEventLoop) e).rebuildSelector();
97 }
98 }
99
100 @Override
101 protected EventExecutor newChild(ThreadFactory threadFactory, Object... args) throws Exception {
102 return new NioEventLoop(this, threadFactory, (SelectorProvider) args[0],
103 ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
104 }
105 }