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;
17  
18  import io.netty.util.concurrent.ExecutorServiceFactory;
19  import io.netty.util.concurrent.MultithreadEventExecutorGroup;
20  import io.netty.util.internal.SystemPropertyUtil;
21  import io.netty.util.internal.logging.InternalLogger;
22  import io.netty.util.internal.logging.InternalLoggerFactory;
23  
24  import java.util.concurrent.Executor;
25  
26  /**
27   * Abstract base class for {@link EventLoopGroup} implementations that handle their tasks with multiple threads at
28   * the same time.
29   */
30  public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup {
31  
32      private static final InternalLogger logger = InternalLoggerFactory.getInstance(MultithreadEventLoopGroup.class);
33  
34      private static final int DEFAULT_EVENT_LOOP_THREADS;
35  
36      static {
37          DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
38                  "io.netty.eventLoopThreads", Runtime.getRuntime().availableProcessors() * 2));
39  
40          if (logger.isDebugEnabled()) {
41              logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
42          }
43      }
44  
45      /**
46       * @see {@link MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, Executor, Object...)}
47       */
48      protected MultithreadEventLoopGroup(int nEventLoops, Executor executor, Object... args) {
49          super(nEventLoops == 0 ? DEFAULT_EVENT_LOOP_THREADS : nEventLoops, executor, args);
50      }
51  
52      /**
53       * @see {@link MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, ExecutorServiceFactory, Object...)}
54       */
55      protected MultithreadEventLoopGroup(int nEventLoops,
56                                          ExecutorServiceFactory executorServiceFactory,
57                                          Object... args) {
58          super(nEventLoops == 0 ? DEFAULT_EVENT_LOOP_THREADS : nEventLoops, executorServiceFactory, args);
59      }
60  
61      @Override
62      public EventLoop next() {
63          return (EventLoop) super.next();
64      }
65  
66      @Override
67      protected abstract EventLoop newChild(Executor executor, Object... args) throws Exception;
68  
69      @Override
70      public ChannelFuture register(Channel channel) {
71          return next().register(channel);
72      }
73  
74      @Override
75      public ChannelFuture register(Channel channel, ChannelPromise promise) {
76          return next().register(channel, promise);
77      }
78  }