Less restriction in shutdown hook modification

This commit is contained in:
Trustin Lee 2012-05-30 04:27:19 -07:00
parent 078a502c5f
commit 4154f42520

View File

@ -221,19 +221,29 @@ public abstract class SingleThreadEventLoop extends AbstractExecutorService impl
return Thread.currentThread() == thread; return Thread.currentThread() == thread;
} }
public void addShutdownHook(Runnable task) { public void addShutdownHook(final Runnable task) {
ensureShutdownHookAccess(); if (inEventLoop()) {
shutdownHooks.add(task); shutdownHooks.add(task);
} else {
execute(new Runnable() {
@Override
public void run() {
shutdownHooks.add(task);
}
});
}
} }
public void removeShutdownHook(Runnable task) { public void removeShutdownHook(final Runnable task) {
ensureShutdownHookAccess(); if (inEventLoop()) {
shutdownHooks.remove(task); shutdownHooks.remove(task);
} } else {
execute(new Runnable() {
private void ensureShutdownHookAccess() { @Override
if (!inEventLoop()) { public void run() {
throw new IllegalStateException("must be called from the event loop"); shutdownHooks.remove(task);
}
});
} }
} }