1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
80
81 @Override
82 @Deprecated
83 public abstract void shutdown();
84
85
86
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
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
178
179
180
181
182
183 @UnstableApi
184 public void lazyExecute(Runnable task) {
185 execute(task);
186 }
187
188
189
190
191
192 @Deprecated
193 public interface LazyRunnable extends Runnable { }
194 }