View Javadoc
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    *   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.util.concurrent;
17  
18  import io.netty.util.internal.UnstableApi;
19  import io.netty.util.internal.logging.InternalLogger;
20  import io.netty.util.internal.logging.InternalLoggerFactory;
21  
22  import org.jetbrains.annotations.Async.Execute;
23  
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.concurrent.AbstractExecutorService;
29  import java.util.concurrent.Callable;
30  import java.util.concurrent.RunnableFuture;
31  import java.util.concurrent.TimeUnit;
32  
33  /**
34   * Abstract base class for {@link EventExecutor} implementations.
35   */
36  public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
37      private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractEventExecutor.class);
38  
39      static final long DEFAULT_SHUTDOWN_QUIET_PERIOD = 2;
40      static final long DEFAULT_SHUTDOWN_TIMEOUT = 15;
41  
42      private final EventExecutorGroup parent;
43      private final Collection<EventExecutor> selfCollection = Collections.<EventExecutor>singleton(this);
44  
45      protected AbstractEventExecutor() {
46          this(null);
47      }
48  
49      protected AbstractEventExecutor(EventExecutorGroup parent) {
50          this.parent = parent;
51      }
52  
53      @Override
54      public EventExecutorGroup parent() {
55          return parent;
56      }
57  
58      @Override
59      public EventExecutor next() {
60          return this;
61      }
62  
63      @Override
64      public boolean inEventLoop() {
65          return inEventLoop(Thread.currentThread());
66      }
67  
68      @Override
69      public Iterator<EventExecutor> iterator() {
70          return selfCollection.iterator();
71      }
72  
73      @Override
74      public Future<?> shutdownGracefully() {
75          return shutdownGracefully(DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
76      }
77  
78      /**
79       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
80       */
81      @Override
82      @Deprecated
83      public abstract void shutdown();
84  
85      /**
86       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
87       */
88      @Override
89      @Deprecated
90      public List<Runnable> shutdownNow() {
91          shutdown();
92          return Collections.emptyList();
93      }
94  
95      @Override
96      public <V> Promise<V> newPromise() {
97          return new DefaultPromise<V>(this);
98      }
99  
100     @Override
101     public <V> ProgressivePromise<V> newProgressivePromise() {
102         return new DefaultProgressivePromise<V>(this);
103     }
104 
105     @Override
106     public <V> Future<V> newSucceededFuture(V result) {
107         return new SucceededFuture<V>(this, result);
108     }
109 
110     @Override
111     public <V> Future<V> newFailedFuture(Throwable cause) {
112         return new FailedFuture<V>(this, cause);
113     }
114 
115     @Override
116     public Future<?> submit(Runnable task) {
117         return (Future<?>) super.submit(task);
118     }
119 
120     @Override
121     public <T> Future<T> submit(Runnable task, T result) {
122         return (Future<T>) super.submit(task, result);
123     }
124 
125     @Override
126     public <T> Future<T> submit(Callable<T> task) {
127         return (Future<T>) super.submit(task);
128     }
129 
130     @Override
131     protected final <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
132         return new PromiseTask<T>(this, runnable, value);
133     }
134 
135     @Override
136     protected final <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
137         return new PromiseTask<T>(this, callable);
138     }
139 
140     @Override
141     public ScheduledFuture<?> schedule(Runnable command, long delay,
142                                        TimeUnit unit) {
143         throw new UnsupportedOperationException();
144     }
145 
146     @Override
147     public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
148         throw new UnsupportedOperationException();
149     }
150 
151     @Override
152     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
153         throw new UnsupportedOperationException();
154     }
155 
156     @Override
157     public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
158         throw new UnsupportedOperationException();
159     }
160 
161     /**
162      * Try to execute the given {@link Runnable} and just log if it throws a {@link Throwable}.
163      */
164     protected static void safeExecute(Runnable task) {
165         try {
166             runTask(task);
167         } catch (Throwable t) {
168             logger.warn("A task raised an exception. Task: {}", task, t);
169         }
170     }
171 
172     protected static void runTask(@Execute Runnable task) {
173         task.run();
174     }
175 
176     /**
177      * Like {@link #execute(Runnable)} but does not guarantee the task will be run until either
178      * a non-lazy task is executed or the executor is shut down.
179      * <p>
180      * The default implementation just delegates to {@link #execute(Runnable)}.
181      * </p>
182      */
183     @UnstableApi
184     public void lazyExecute(Runnable task) {
185         execute(task);
186     }
187 
188     /**
189      *  @deprecated override {@link SingleThreadEventExecutor#wakesUpForTask} to re-create this behaviour
190      *
191      */
192     @Deprecated
193     public interface LazyRunnable extends Runnable { }
194 }