1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.util;
17
18 import java.util.Collections;
19 import java.util.IdentityHashMap;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.concurrent.AbstractExecutorService;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.RejectedExecutionException;
26 import java.util.concurrent.TimeUnit;
27
28 import org.jboss.netty.channel.ChannelFactory;
29 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public class VirtualExecutorService extends AbstractExecutorService {
82
83 private final Executor e;
84 private final ExecutorService s;
85 final Object startStopLock = new Object();
86 volatile boolean shutdown;
87 Set<Thread> activeThreads = new MapBackedSet<Thread>(new IdentityHashMap<Thread, Boolean>());
88
89
90
91
92 public VirtualExecutorService(Executor parent) {
93 if (parent == null) {
94 throw new NullPointerException("parent");
95 }
96
97 if (parent instanceof ExecutorService) {
98 e = null;
99 s = (ExecutorService) parent;
100 } else {
101 e = parent;
102 s = null;
103 }
104 }
105
106 public boolean isShutdown() {
107 synchronized (startStopLock) {
108 return shutdown;
109 }
110 }
111
112 public boolean isTerminated() {
113 synchronized (startStopLock) {
114 return shutdown && activeThreads.isEmpty();
115 }
116 }
117
118 public void shutdown() {
119 synchronized (startStopLock) {
120 if (shutdown) {
121 return;
122 }
123 shutdown = true;
124 }
125 }
126
127 public List<Runnable> shutdownNow() {
128 synchronized (startStopLock) {
129 if (!isTerminated()) {
130 shutdown();
131 for (Thread t: activeThreads) {
132 t.interrupt();
133 }
134 }
135 }
136
137 return Collections.emptyList();
138 }
139
140 public boolean awaitTermination(long timeout, TimeUnit unit)
141 throws InterruptedException {
142 synchronized (startStopLock) {
143 while (!isTerminated()) {
144 startStopLock.wait(TimeUnit.MILLISECONDS.convert(timeout, unit));
145 }
146
147 return isTerminated();
148 }
149 }
150
151 public void execute(Runnable command) {
152 if (command == null) {
153 throw new NullPointerException("command");
154 }
155
156 if (shutdown) {
157 throw new RejectedExecutionException();
158 }
159
160 if (s != null) {
161 s.execute(new ChildExecutorRunnable(command));
162 } else {
163 e.execute(new ChildExecutorRunnable(command));
164 }
165 }
166
167 private class ChildExecutorRunnable implements Runnable {
168
169 private final Runnable runnable;
170
171 ChildExecutorRunnable(Runnable runnable) {
172 this.runnable = runnable;
173 }
174
175 public void run() {
176 Thread thread = Thread.currentThread();
177 synchronized (startStopLock) {
178 activeThreads.add(thread);
179 }
180 try {
181 runnable.run();
182 } finally {
183 synchronized (startStopLock) {
184 boolean removed = activeThreads.remove(thread);
185 assert removed;
186 if (isTerminated()) {
187 startStopLock.notifyAll();
188 }
189 }
190 }
191 }
192 }
193 }