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 Iterator<EventExecutor> iterator() {
65          return selfCollection.iterator();
66      }
67  
68      @Override
69      public Future<?> shutdownGracefully() {
70          return shutdownGracefully(DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
71      }
72  
73      /**
74       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
75       */
76      @Override
77      @Deprecated
78      public abstract void shutdown();
79  
80      /**
81       * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
82       */
83      @Override
84      @Deprecated
85      public List<Runnable> shutdownNow() {
86          shutdown();
87          return Collections.emptyList();
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 
136     /**
137      * Try to execute the given {@link Runnable} and just log if it throws a {@link Throwable}.
138      */
139     protected static void safeExecute(Runnable task) {
140         try {
141             runTask(task);
142         } catch (Throwable t) {
143             logger.warn("A task raised an exception. Task: {}", task, t);
144         }
145     }
146 
147     protected static void runTask(@Execute Runnable task) {
148         task.run();
149     }
150 
151     /**
152      * Like {@link #execute(Runnable)} but does not guarantee the task will be run until either
153      * a non-lazy task is executed or the executor is shut down.
154      * <p>
155      * The default implementation just delegates to {@link #execute(Runnable)}.
156      * </p>
157      */
158     @UnstableApi
159     public void lazyExecute(Runnable task) {
160         execute(task);
161     }
162 
163     /**
164      *  @deprecated override {@link SingleThreadEventExecutor#wakesUpForTask} to re-create this behaviour
165      *
166      */
167     @Deprecated
168     public interface LazyRunnable extends Runnable { }
169 }