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