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 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
75
76 @Override
77 @Deprecated
78 public abstract void shutdown();
79
80
81
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
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
153
154
155
156
157
158 @UnstableApi
159 public void lazyExecute(Runnable task) {
160 execute(task);
161 }
162
163
164
165
166
167 @Deprecated
168 public interface LazyRunnable extends Runnable { }
169 }