查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2013 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.util.concurrent;
17  
18  import java.util.Collections;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Set;
22  import java.util.concurrent.AbstractExecutorService;
23  import java.util.concurrent.Callable;
24  import java.util.concurrent.RunnableFuture;
25  import java.util.concurrent.TimeUnit;
26  
27  /**
28   * Abstract base class for {@link EventExecutor} implementations.
29   */
30  public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
31      private final Set<EventExecutor> executorSet = Collections.singleton((EventExecutor) this);
32  
33      @Override
34      public EventExecutor next() {
35          return this;
36      }
37  
38      @Override
39      public boolean inEventLoop() {
40          return inEventLoop(Thread.currentThread());
41      }
42  
43      @Override
44      public Iterator<EventExecutor> iterator() {
45          return executorSet.iterator();
46      }
47  
48      @Override
49      public Future<?> shutdownGracefully() {
50          return shutdownGracefully(2, 15, TimeUnit.SECONDS);
51      }
52  
53      /**
54       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
55       */
56      @Override
57      @Deprecated
58      public abstract void shutdown();
59  
60      /**
61       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
62       */
63      @Override
64      @Deprecated
65      public List<Runnable> shutdownNow() {
66          shutdown();
67          return Collections.emptyList();
68      }
69  
70      @Override
71      public <V> Promise<V> newPromise() {
72          return new DefaultPromise<V>(this);
73      }
74  
75      @Override
76      public <V> ProgressivePromise<V> newProgressivePromise() {
77          return new DefaultProgressivePromise<V>(this);
78      }
79  
80      @Override
81      public <V> Future<V> newSucceededFuture(V result) {
82          return new SucceededFuture<V>(this, result);
83      }
84  
85      @Override
86      public <V> Future<V> newFailedFuture(Throwable cause) {
87          return new FailedFuture<V>(this, cause);
88      }
89  
90      @Override
91      public Future<?> submit(Runnable task) {
92          return (Future<?>) super.submit(task);
93      }
94  
95      @Override
96      public <T> Future<T> submit(Runnable task, T result) {
97          return (Future<T>) super.submit(task, result);
98      }
99  
100     @Override
101     public <T> Future<T> submit(Callable<T> task) {
102         return (Future<T>) super.submit(task);
103     }
104 
105     @Override
106     protected final <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
107         return new PromiseTask<T>(this, runnable, value);
108     }
109 
110     @Override
111     protected final <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
112         return new PromiseTask<T>(this, callable);
113     }
114 
115     @Override
116     public ScheduledFuture<?> schedule(Runnable command, long delay,
117                                        TimeUnit unit) {
118         throw new UnsupportedOperationException();
119     }
120 
121     @Override
122     public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
123         throw new UnsupportedOperationException();
124     }
125 
126     @Override
127     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
128         throw new UnsupportedOperationException();
129     }
130 
131     @Override
132     public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
133         throw new UnsupportedOperationException();
134     }
135 }