From 5f27babff4d3e11f9b38aa85b71800891fda0a9d Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 3 Jun 2009 03:30:01 +0000 Subject: [PATCH] Resolved issue: NETTY-161 Provide a way to get notified when all I/O workers of a ChannelFactory are terminated. * Added VirtualExecutorService --- .../netty/util/VirtualExecutorService.java | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 src/main/java/org/jboss/netty/util/VirtualExecutorService.java diff --git a/src/main/java/org/jboss/netty/util/VirtualExecutorService.java b/src/main/java/org/jboss/netty/util/VirtualExecutorService.java new file mode 100644 index 0000000000..2a62f6d0e4 --- /dev/null +++ b/src/main/java/org/jboss/netty/util/VirtualExecutorService.java @@ -0,0 +1,270 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.util; + +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Set; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.jboss.netty.util.internal.MapBackedSet; + +/** + * A delegating {@link ExecutorService} with its own termination management. + *

+ * {@link VirtualExecutorService} is used when you want to inject an + * {@link ExecutorService} but you do not want to allow the explicit termination + * of threads on shutdown request. It is particularly useful when the + * {@link ExecutorService} to inject is shared by different components and + * the life cycle of the components depend on the termination of the injected + * {@link ExecutorService}. + * + *

+ * ExecutorService globalExecutor = ...;
+ * ExecutorService virtualExecutor = new VirtualExecutorService(globalExecutor);
+ *
+ * ChannelFactory factory =
+ *         new NioServerSocketChannelFactory(virtualExecutor, virtualExecutor);
+ * ...
+ *
+ * // ChannelFactory.releaseExternalResources() shuts down the executor and
+ * // interrupts the I/O threads to terminate all I/O tasks and to release all
+ * // resources acquired by ChannelFactory.
+ * factory.releaseExternalResources();
+ *
+ * // Note that globalExecutor is not shut down because VirtualExecutorService
+ * // implements its own termination management. All threads which were acquired
+ * // by ChannelFactory via VirtualExecutorService are returned to the pool.
+ * assert !globalExecutor.isShutdown();
+ * 
+ * + *

The differences from an ordinary {@link ExecutorService}

+ * + * A shutdown request ({@link #shutdown()} or {@link #shutdownNow()}) does not + * shut down its parent {@link Executor} but simply sets its internal flag to + * reject further execution request. + *

+ * {@link #shutdownNow()} interrupts only the thread which is executing the + * task executed via {@link VirtualExecutorService}. + *

+ * {@link #awaitTermination(long, TimeUnit)} does not wait for real thread + * termination but wait until {@link VirtualExecutorService} is shut down and + * its active tasks are finished and the threads are returned to the parent + * {@link Executor}. + * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public class VirtualExecutorService extends AbstractExecutorService { + + private final Executor e; + private final ExecutorService s; + final Object startStopLock = new Object(); + volatile boolean shutdown; + Set activeThreads = new MapBackedSet(new IdentityHashMap()); + + /** + * Creates a new instance with the specified parent {@link Executor}. + */ + public VirtualExecutorService(Executor parent) { + if (parent == null) { + throw new NullPointerException("parent"); + } + + if (parent instanceof ExecutorService) { + e = null; + s = (ExecutorService) parent; + } else { + e = parent; + s = null; + } + } + + public boolean isShutdown() { + synchronized (startStopLock) { + return shutdown; + } + } + + public boolean isTerminated() { + synchronized (startStopLock) { + return shutdown && activeThreads.isEmpty(); + } + } + + public void shutdown() { + synchronized (startStopLock) { + if (shutdown) { + return; + } + shutdown = true; + } + } + + public List shutdownNow() { + synchronized (startStopLock) { + if (!isTerminated()) { + shutdown(); + for (Thread t: activeThreads) { + t.interrupt(); + } + } + } + + return Collections.emptyList(); + } + + public boolean awaitTermination(long timeout, TimeUnit unit) + throws InterruptedException { + synchronized (startStopLock) { + while (!isTerminated()) { + startStopLock.wait(TimeUnit.MILLISECONDS.convert(timeout, unit)); + } + + return isTerminated(); + } + } + + @Override + public List> invokeAll(Collection> tasks) + throws InterruptedException { + if (s != null) { + return s.invokeAll(tasks); + } else { + return super.invokeAll(tasks); + } + } + + @Override + public List> invokeAll( + Collection> tasks, long timeout, TimeUnit unit) + throws InterruptedException { + if (s != null) { + return s.invokeAll(tasks, timeout, unit); + } else { + return super.invokeAll(tasks, timeout, unit); + } + } + + @Override + public T invokeAny(Collection> tasks) + throws InterruptedException, ExecutionException { + if (s != null) { + return s.invokeAny(tasks); + } else { + return super.invokeAny(tasks); + } + } + + @Override + public T invokeAny(Collection> tasks, + long timeout, TimeUnit unit) throws InterruptedException, + ExecutionException, TimeoutException { + if (s != null) { + return s.invokeAny(tasks, timeout, unit); + } else { + return super.invokeAny(tasks, timeout, unit); + } + } + + @Override + public Future submit(Callable task) { + if (s != null) { + return s.submit(task); + } else { + return super.submit(task); + } + } + + @Override + public Future submit(Runnable task) { + if (s != null) { + return s.submit(task); + } else { + return super.submit(task); + } + } + + @Override + public Future submit(Runnable task, T result) { + if (s != null) { + return s.submit(task, result); + } else { + return super.submit(task, result); + } + } + + public void execute(Runnable command) { + if (command == null) { + throw new NullPointerException("command"); + } + + if (shutdown) { + throw new RejectedExecutionException(); + } + + if (s != null) { + s.execute(new ChildExecutorRunnable(command)); + } else { + e.execute(new ChildExecutorRunnable(command)); + } + } + + private class ChildExecutorRunnable implements Runnable { + + private final Runnable runnable; + + ChildExecutorRunnable(Runnable runnable) { + this.runnable = runnable; + } + + public void run() { + Thread thread = Thread.currentThread(); + synchronized (startStopLock) { + activeThreads.add(thread); + } + try { + runnable.run(); + } finally { + synchronized (startStopLock) { + boolean removed = activeThreads.remove(thread); + assert removed; + if (isTerminated()) { + startStopLock.notifyAll(); + } + } + } + } + } +}