View Javadoc
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.EventLoop;
20  import io.netty.channel.MultithreadEventLoopGroup;
21  import io.netty.util.concurrent.EventExecutor;
22  import io.netty.util.concurrent.ExecutorServiceFactory;
23  
24  import java.nio.channels.Selector;
25  import java.nio.channels.spi.SelectorProvider;
26  import java.util.concurrent.Executor;
27  
28  /**
29   * A {@link MultithreadEventLoopGroup} implementation which is used for NIO {@link Selector} based {@link Channel}s.
30   */
31  public class NioEventLoopGroup extends MultithreadEventLoopGroup {
32  
33      /**
34       * Create a new instance that uses twice as many {@link EventLoop}s as there processors/cores
35       * available, as well as the default {@link Executor} and the {@link SelectorProvider} which
36       * is returned by {@link SelectorProvider#provider()}.
37       *
38       * @see io.netty.util.concurrent.DefaultExecutorServiceFactory
39       */
40      public NioEventLoopGroup() {
41          this(0);
42      }
43  
44      /**
45       * Create a new instance that uses the default {@link Executor} and the {@link SelectorProvider} which
46       * is returned by {@link SelectorProvider#provider()}.
47       *
48       * @see io.netty.util.concurrent.DefaultExecutorServiceFactory
49       *
50       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
51       *                      If {@code executor} is {@code null} this number will also be the parallelism
52       *                      requested from the default executor. It is generally advised for the number
53       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
54       */
55      public NioEventLoopGroup(int nEventLoops) {
56          this(nEventLoops, (Executor) null);
57      }
58  
59      /**
60       * Create a new instance that uses the the {@link SelectorProvider} which is returned by
61       * {@link SelectorProvider#provider()}.
62       *
63       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
64       *                      If {@code executor} is {@code null} this number will also be the parallelism
65       *                      requested from the default executor. It is generally advised for the number
66       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
67       *                      {@code executor} to lie very close together.
68       * @param executor   the {@link Executor} to use, or {@code null} if the default should be used.
69       */
70      public NioEventLoopGroup(int nEventLoops, Executor executor) {
71          this(nEventLoops, executor, SelectorProvider.provider());
72      }
73  
74      /**
75       * Create a new instance that uses the the {@link SelectorProvider} which is returned by
76       * {@link SelectorProvider#provider()}.
77       *
78       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
79       *                      If {@code executor} is {@code null} this number will also be the parallelism
80       *                      requested from the default executor. It is generally advised for the number
81       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
82       *                      {@code executor} to lie very close together.
83       * @param executorServiceFactory   the {@link ExecutorServiceFactory} to use, or {@code null} if the default
84       *                                 should be used.
85       */
86      public NioEventLoopGroup(int nEventLoops, ExecutorServiceFactory executorServiceFactory) {
87          this(nEventLoops, executorServiceFactory, SelectorProvider.provider());
88      }
89  
90      /**
91       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
92       *                      If {@code executor} is {@code null} this number will also be the parallelism
93       *                      requested from the default executor. It is generally advised for the number
94       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
95       *                      {@code executor} to lie very close together.
96       * @param executor  the {@link Executor} to use, or {@code null} if the default should be used.
97       * @param selectorProvider  the {@link SelectorProvider} to use. This value must not be {@code null}.
98       */
99      public NioEventLoopGroup(int nEventLoops, Executor executor, final SelectorProvider selectorProvider) {
100         super(nEventLoops, executor, selectorProvider);
101     }
102 
103     /**
104      * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
105      *                      If {@code executor} is {@code null} this number will also be the parallelism
106      *                      requested from the default executor. It is generally advised for the number
107      *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
108      *                      {@code executor} to lie very close together.
109      * @param executorServiceFactory   the {@link ExecutorServiceFactory} to use, or {@code null} if the
110      *                                 default should be used.
111      * @param selectorProvider  the {@link SelectorProvider} to use. This value must not be {@code null}.
112      */
113     public NioEventLoopGroup(
114             int nEventLoops, ExecutorServiceFactory executorServiceFactory, final SelectorProvider selectorProvider) {
115         super(nEventLoops, executorServiceFactory, selectorProvider);
116     }
117 
118     /**
119      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
120      * {@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.
121      */
122     public void setIoRatio(int ioRatio) {
123         for (EventExecutor e: children()) {
124             ((NioEventLoop) e).setIoRatio(ioRatio);
125         }
126     }
127 
128     /**
129      * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work
130      * around the  infamous epoll 100% CPU bug.
131      */
132     public void rebuildSelectors() {
133         for (EventExecutor e: children()) {
134             ((NioEventLoop) e).rebuildSelector();
135         }
136     }
137 
138     @Override
139     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
140         return new NioEventLoop(this, executor, (SelectorProvider) args[0]);
141     }
142 }