查看本类的 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;
17  
18  /**
19   * {@link SingleThreadEventLoop} which is used to handle OIO {@link Channel}'s. So in general there will be
20   * one {@link ThreadPerChannelEventLoop} per {@link Channel}.
21   *
22   * @deprecated this will be remove in the next-major release.
23   */
24  @Deprecated
25  public class ThreadPerChannelEventLoop extends SingleThreadEventLoop {
26  
27      private final ThreadPerChannelEventLoopGroup parent;
28      private Channel ch;
29  
30      public ThreadPerChannelEventLoop(ThreadPerChannelEventLoopGroup parent) {
31          super(parent, parent.executor, true);
32          this.parent = parent;
33      }
34  
35      @Override
36      public ChannelFuture register(ChannelPromise promise) {
37          return super.register(promise).addListener(new ChannelFutureListener() {
38              @Override
39              public void operationComplete(ChannelFuture future) throws Exception {
40                  if (future.isSuccess()) {
41                      ch = future.channel();
42                  } else {
43                      deregister();
44                  }
45              }
46          });
47      }
48  
49      @Deprecated
50      @Override
51      public ChannelFuture register(Channel channel, ChannelPromise promise) {
52          return super.register(channel, promise).addListener(new ChannelFutureListener() {
53              @Override
54              public void operationComplete(ChannelFuture future) throws Exception {
55                  if (future.isSuccess()) {
56                      ch = future.channel();
57                  } else {
58                      deregister();
59                  }
60              }
61          });
62      }
63  
64      @Override
65      protected void run() {
66          for (;;) {
67              Runnable task = takeTask();
68              if (task != null) {
69                  task.run();
70                  updateLastExecutionTime();
71              }
72  
73              Channel ch = this.ch;
74              if (isShuttingDown()) {
75                  if (ch != null) {
76                      ch.unsafe().close(ch.unsafe().voidPromise());
77                  }
78                  if (confirmShutdown()) {
79                      break;
80                  }
81              } else {
82                  if (ch != null) {
83                      // Handle deregistration
84                      if (!ch.isRegistered()) {
85                          runAllTasks();
86                          deregister();
87                      }
88                  }
89              }
90          }
91      }
92  
93      protected void deregister() {
94          ch = null;
95          parent.activeChildren.remove(this);
96          parent.idleChildren.add(this);
97      }
98  
99      @Override
100     public int registeredChannels() {
101         return 1;
102     }
103 }