查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.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   * {@link MultithreadEventLoopGroup} implementations which is used for NIO {@link Selector} based {@link Channel}s.
37   */
38  public class NioEventLoopGroup extends MultithreadEventLoopGroup {
39  
40      /**
41       * Create a new instance using the default number of threads, the default {@link ThreadFactory} and
42       * the {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
43       */
44      public NioEventLoopGroup() {
45          this(0);
46      }
47  
48      /**
49       * Create a new instance using the specified number of threads, {@link ThreadFactory} and the
50       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
51       */
52      public NioEventLoopGroup(int nThreads) {
53          this(nThreads, (Executor) null);
54      }
55  
56      /**
57       * Create a new instance using the default number of threads, the given {@link ThreadFactory} and the
58       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
59       */
60      public NioEventLoopGroup(ThreadFactory threadFactory) {
61          this(0, threadFactory, SelectorProvider.provider());
62      }
63  
64      /**
65       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the
66       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
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       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
78       * {@link SelectorProvider}.
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      * @param nThreads the number of threads that will be used by this instance.
125      * @param executor the Executor to use, or {@code null} if default one should be used.
126      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
127      * @param selectorProvider the {@link SelectorProvider} to use.
128      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
129      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
130      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
131      *                         {@link SingleThreadEventLoop#execute(Runnable)},
132      *                         or {@code null} if default one should be used.
133      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
134      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
135      *                             or {@code null} if default one should be used.
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      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
149      * {@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.
150      */
151     public void setIoRatio(int ioRatio) {
152         for (EventExecutor e: this) {
153             ((NioEventLoop) e).setIoRatio(ioRatio);
154         }
155     }
156 
157     /**
158      * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work
159      * around the  infamous epoll 100% CPU bug.
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 }