2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util.concurrent;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.util.concurrent.AbstractListeningExecutorService;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.List;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
25 * An {@link com.google.common.util.concurrent.ListeningExecutorService} implementation that also allows
26 * for an {@link Executor} to be specified on construction that is used to execute {@link ListenableFuture} callback
27 * Runnables, registered via {@link com.google.common.util.concurrent.Futures#addCallback} or
28 * {@link ListenableFuture#addListener} directly, asynchronously when a task that is run on this executor completes.
29 * This is useful when you want to guarantee listener callback executions are off-loaded onto another thread to avoid
30 * blocking the thread that completed the task, as a common use case is to pass an executor that runs tasks in the same
31 * thread as the caller (i.e. {@code MoreExecutors#sameThreadExecutor}) to {@link ListenableFuture#addListener}.
34 * Most commonly, this class would be used in lieu of {@code MoreExecutors#listeningDecorator} when the underlying
35 * delegate Executor is single-threaded, in which case, you may not want ListenableFuture callbacks to block the single
39 * Note: the Executor specified on construction does not replace the Executor specified
40 * in {@link ListenableFuture#addListener}. The latter Executor is still used however, if it is detected that
41 * the listener Runnable would execute in the thread that completed the task, the listener is executed on Executor
42 * specified on construction.
44 * @author Thomas Pantelis
45 * @see AsyncNotifyingListenableFutureTask
47 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
48 private final @NonNull ExecutorService delegate;
49 private final @Nullable Executor listenableFutureExecutor;
54 * @param delegate the back-end ExecutorService.
55 * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
56 * If null, no executor is used.
58 public AsyncNotifyingListeningExecutorService(final @NonNull ExecutorService delegate,
59 @Nullable final Executor listenableFutureExecutor) {
60 this.delegate = requireNonNull(delegate);
61 this.listenableFutureExecutor = listenableFutureExecutor;
65 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
67 * @param task the Runnable to execute
69 private <T> @NonNull AsyncNotifyingListenableFutureTask<T> newFutureTask(final @NonNull Runnable task,
71 return AsyncNotifyingListenableFutureTask.create(task, result, listenableFutureExecutor);
75 * Returns the delegate ExecutorService.
77 protected @NonNull ExecutorService getDelegate() {
82 public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
83 return delegate.awaitTermination(timeout, unit);
87 public boolean isShutdown() {
88 return delegate.isShutdown();
92 public boolean isTerminated() {
93 return delegate.isTerminated();
97 public void shutdown() {
102 public List<Runnable> shutdownNow() {
103 return delegate.shutdownNow();
107 public void execute(final Runnable command) {
108 delegate.execute(command);
112 public <T> ListenableFuture<T> submit(final Callable<T> task) {
113 final AsyncNotifyingListenableFutureTask<T> futureTask = AsyncNotifyingListenableFutureTask.create(
114 requireNonNull(task), listenableFutureExecutor);
115 delegate.execute(futureTask);
120 public ListenableFuture<?> submit(final Runnable task) {
121 final AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask(requireNonNull(task), null);
122 delegate.execute(futureTask);
127 public <T> ListenableFuture<T> submit(final Runnable task, final T result) {
128 final AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask(requireNonNull(task), result);
129 delegate.execute(futureTask);
133 protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
134 return toStringHelper;
138 public final String toString() {
139 return addToStringAttributes(MoreObjects.toStringHelper(this).add("delegate", delegate)).toString();