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.Nonnull;
22 import javax.annotation.Nullable;
25 * An {@link com.google.common.util.concurrent.ListeningExecutorService}
26 * implementation that also allows for an {@link Executor} to be specified on
27 * construction that is used to execute {@link ListenableFuture} callback
28 * Runnables, registered via
29 * {@link com.google.common.util.concurrent.Futures#addCallback} or
30 * {@link ListenableFuture#addListener} directly, asynchronously when a task
31 * that is run on this executor completes. This is useful when you want to
32 * guarantee listener callback executions are off-loaded onto another thread to
33 * avoid blocking the thread that completed the task, as a common use case is to
34 * pass an executor that runs tasks in the same thread as the caller (ie
35 * <code>MoreExecutors#sameThreadExecutor</code>}) to
36 * {@link ListenableFuture#addListener}.
39 * Most commonly, this class would be used in lieu of
40 * <code>MoreExecutors#listeningDecorator</code> when the underlying delegate
41 * Executor is single-threaded, in which case, you may not want ListenableFuture
42 * callbacks to block the single thread.
45 * Note: the Executor specified on construction does not replace the Executor
46 * specified in {@link ListenableFuture#addListener}. The latter Executor is
47 * still used however, if it is detected that the listener Runnable would
48 * execute in the thread that completed the task, the listener is executed on
49 * Executor specified on construction.
51 * @author Thomas Pantelis
52 * @see AsyncNotifyingListenableFutureTask
54 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
56 private final ExecutorService delegate;
57 private final Executor listenableFutureExecutor;
62 * @param delegate the back-end ExecutorService.
63 * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
64 * If null, no executor is used.
66 public AsyncNotifyingListeningExecutorService( final ExecutorService delegate,
67 @Nullable final Executor listenableFutureExecutor ) {
68 this.delegate = Preconditions.checkNotNull( delegate );
69 this.listenableFutureExecutor = listenableFutureExecutor;
73 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
75 * @param task the Callable to execute
77 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Callable<T> task ) {
78 return AsyncNotifyingListenableFutureTask.create( task, listenableFutureExecutor );
82 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
84 * @param task the Runnable to execute
86 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Runnable task, final T result ) {
87 return AsyncNotifyingListenableFutureTask.create( task, result, listenableFutureExecutor );
91 * Returns the delegate ExecutorService.
93 protected ExecutorService getDelegate() {
98 public boolean awaitTermination( final long timeout, @Nonnull final TimeUnit unit ) throws InterruptedException {
99 return delegate.awaitTermination( timeout, unit );
103 public boolean isShutdown() {
104 return delegate.isShutdown();
108 public boolean isTerminated() {
109 return delegate.isTerminated();
113 public void shutdown() {
119 public List<Runnable> shutdownNow() {
120 return delegate.shutdownNow();
124 public void execute( @Nonnull final Runnable command ) {
125 delegate.execute( command );
130 public <T> ListenableFuture<T> submit( final Callable<T> task ) {
131 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task );
132 delegate.execute( futureTask );
138 public ListenableFuture<?> submit( final Runnable task ) {
139 AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask( task, null );
140 delegate.execute( futureTask );
146 public <T> ListenableFuture<T> submit( final Runnable task, final T result ) {
147 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task, result );
148 delegate.execute( futureTask );
152 protected ToStringHelper addToStringAttributes( final ToStringHelper toStringHelper ) {
153 return toStringHelper;
157 public final String toString() {
158 return addToStringAttributes( MoreObjects.toStringHelper( this )
159 .add( "delegate", delegate ) ).toString();