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
9 package org.opendaylight.yangtools.util.concurrent;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
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 javax.annotation.Nullable;
24 * An {@link com.google.common.util.concurrent.ListeningExecutorService}
25 * implementation that also allows for an {@link Executor} to be specified on
26 * construction that is used to execute {@link ListenableFuture} callback
27 * Runnables, registered via
28 * {@link com.google.common.util.concurrent.Futures#addCallback} or
29 * {@link ListenableFuture#addListener} directly, asynchronously when a task
30 * that is run on this executor completes. This is useful when you want to
31 * guarantee listener callback executions are off-loaded onto another thread to
32 * avoid blocking the thread that completed the task, as a common use case is to
33 * pass an executor that runs tasks in the same thread as the caller (ie
34 * <code>MoreExecutors#sameThreadExecutor</code>}) to
35 * {@link ListenableFuture#addListener}.
38 * Most commonly, this class would be used in lieu of
39 * <code>MoreExecutors#listeningDecorator</code> when the underlying delegate
40 * Executor is single-threaded, in which case, you may not want ListenableFuture
41 * callbacks to block the single thread.
44 * Note: the Executor specified on construction does not replace the Executor
45 * specified in {@link ListenableFuture#addListener}. The latter Executor is
46 * still used however, if it is detected that the listener Runnable would
47 * execute in the thread that completed the task, the listener is executed on
48 * Executor specified on construction.
50 * @author Thomas Pantelis
51 * @see AsyncNotifyingListenableFutureTask
53 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
55 private final ExecutorService delegate;
56 private final Executor listenableFutureExecutor;
61 * @param delegate the back-end ExecutorService.
62 * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
63 * If null, no executor is used.
65 public AsyncNotifyingListeningExecutorService( final ExecutorService delegate,
66 @Nullable final Executor listenableFutureExecutor ) {
67 this.delegate = Preconditions.checkNotNull( delegate );
68 this.listenableFutureExecutor = listenableFutureExecutor;
72 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
74 * @param task the Callable to execute
76 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Callable<T> task ) {
77 return AsyncNotifyingListenableFutureTask.create( task, listenableFutureExecutor );
81 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
83 * @param task the Runnable to execute
85 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Runnable task, final T result ) {
86 return AsyncNotifyingListenableFutureTask.create( task, result, listenableFutureExecutor );
90 * Returns the delegate ExecutorService.
92 protected ExecutorService getDelegate() {
97 public boolean awaitTermination( final long timeout, final TimeUnit unit ) throws InterruptedException {
98 return delegate.awaitTermination( timeout, unit );
102 public boolean isShutdown() {
103 return delegate.isShutdown();
107 public boolean isTerminated() {
108 return delegate.isTerminated();
112 public void shutdown() {
117 public List<Runnable> shutdownNow() {
118 return delegate.shutdownNow();
122 public void execute( final Runnable command ) {
123 delegate.execute( command );
127 public <T> ListenableFuture<T> submit( final Callable<T> task ) {
128 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task );
129 delegate.execute( futureTask );
134 public ListenableFuture<?> submit( final Runnable task ) {
135 AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask( task, null );
136 delegate.execute( futureTask );
141 public <T> ListenableFuture<T> submit( final Runnable task, final T result ) {
142 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task, result );
143 delegate.execute( futureTask );
147 protected ToStringHelper addToStringAttributes( final ToStringHelper toStringHelper ) {
148 return toStringHelper;
152 public final String toString() {
153 return addToStringAttributes( MoreObjects.toStringHelper( this )
154 .add( "delegate", delegate ) ).toString();