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.DefaultSelectStrategyFactory;
21  import io.netty.channel.MultithreadEventLoopGroup;
22  import io.netty.channel.SelectStrategyFactory;
23  import io.netty.util.concurrent.EventExecutor;
24  import io.netty.util.concurrent.EventExecutorChooserFactory;
25  import io.netty.util.concurrent.RejectedExecutionHandler;
26  import io.netty.util.concurrent.RejectedExecutionHandlers;
27  
28  import java.nio.channels.Selector;
29  import java.nio.channels.spi.SelectorProvider;
30  import java.util.concurrent.Executor;
31  import java.util.concurrent.ThreadFactory;
32  
33  /**
34   * {@link MultithreadEventLoopGroup} implementations which is used for NIO {@link Selector} based {@link Channel}s.
35   */
36  public class NioEventLoopGroup extends MultithreadEventLoopGroup {
37  
38      /**
39       * Create a new instance using the default number of threads, the default {@link ThreadFactory} and
40       * the {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
41       */
42      public NioEventLoopGroup() {
43          this(0);
44      }
45  
46      /**
47       * Create a new instance using the specified number of threads, {@link ThreadFactory} and the
48       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
49       */
50      public NioEventLoopGroup(int nThreads) {
51          this(nThreads, (Executor) null);
52      }
53  
54      /**
55       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the
56       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
57       */
58      public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
59          this(nThreads, threadFactory, SelectorProvider.provider());
60      }
61  
62      public NioEventLoopGroup(int nThreads, Executor executor) {
63          this(nThreads, executor, SelectorProvider.provider());
64      }
65  
66      /**
67       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
68       * {@link SelectorProvider}.
69       */
70      public NioEventLoopGroup(
71              int nThreads, ThreadFactory threadFactory, final SelectorProvider selectorProvider) {
72          this(nThreads, threadFactory, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
73      }
74  
75      public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,
76          final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {
77          super(nThreads, threadFactory, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
78      }
79  
80      public NioEventLoopGroup(
81              int nThreads, Executor executor, final SelectorProvider selectorProvider) {
82          this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
83      }
84  
85      public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
86                               final SelectStrategyFactory selectStrategyFactory) {
87          super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
88      }
89  
90      public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
91                               final SelectorProvider selectorProvider,
92                               final SelectStrategyFactory selectStrategyFactory) {
93          super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory,
94                  RejectedExecutionHandlers.reject());
95      }
96  
97      public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
98                               final SelectorProvider selectorProvider,
99                               final SelectStrategyFactory selectStrategyFactory,
100                              final RejectedExecutionHandler rejectedExecutionHandler) {
101         super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory, rejectedExecutionHandler);
102     }
103 
104     /**
105      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
106      * {@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.
107      */
108     public void setIoRatio(int ioRatio) {
109         for (EventExecutor e: this) {
110             ((NioEventLoop) e).setIoRatio(ioRatio);
111         }
112     }
113 
114     /**
115      * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work
116      * around the  infamous epoll 100% CPU bug.
117      */
118     public void rebuildSelectors() {
119         for (EventExecutor e: this) {
120             ((NioEventLoop) e).rebuildSelector();
121         }
122     }
123 
124     @Override
125     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
126         return new NioEventLoop(this, executor, (SelectorProvider) args[0],
127             ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
128     }
129 }