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