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 java.util.List;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.Executor;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nullable;
19 import com.google.common.base.Objects;
20 import com.google.common.base.Objects.ToStringHelper;
21 import com.google.common.base.Preconditions;
22 import com.google.common.util.concurrent.AbstractListeningExecutorService;
23 import com.google.common.util.concurrent.ListenableFuture;
26 * An {@link ListeningExecutorService} implementation that also allows for an {@link Executor} to be
27 * specified on construction that is used to execute {@link ListenableFuture} callback Runnables,
28 * registered via {@link Futures#addCallback} or {@link ListenableFuture#addListener} directly,
29 * asynchronously when a task that is run on this executor completes. This is useful when you want
30 * to guarantee listener callback executions are off-loaded onto another thread to avoid blocking
31 * the thread that completed the task, as a common use case is to pass an executor that runs tasks
32 * in the same thread as the caller (ie <code>MoreExecutors#sameThreadExecutor</code>}) to
33 * {@link ListenableFuture#addListener}.
35 * Most commonly, this class would be used in lieu of <code>MoreExecutors#listeningDecorator<code>
36 * when the underlying delegate Executor is single-threaded, in which case, you may not want
37 * ListenableFuture callbacks to block the single thread.
39 * Note: the Executor specified on construction does not replace the Executor specified in
40 * {@link ListenableFuture#addListener}. The latter Executor is still used however, if it is
41 * detected that the listener Runnable would execute in the thread that completed the task, the
42 * listener is executed on Executor specified on construction.
44 * @author Thomas Pantelis
45 * @see AsyncNotifyingListenableFutureTask
47 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
49 private final ExecutorService delegate;
50 private final Executor listenableFutureExecutor;
55 * @param delegate the back-end ExecutorService.
56 * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
57 * If null, no executor is used.
59 public AsyncNotifyingListeningExecutorService( ExecutorService delegate,
60 @Nullable Executor listenableFutureExecutor ) {
61 this.delegate = Preconditions.checkNotNull( delegate );
62 this.listenableFutureExecutor = listenableFutureExecutor;
66 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
68 * @param task the Callable to execute
70 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( Callable<T> task ) {
71 return AsyncNotifyingListenableFutureTask.create( task, listenableFutureExecutor );
75 * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
77 * @param task the Runnable to execute
79 private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( Runnable task, T result ) {
80 return AsyncNotifyingListenableFutureTask.create( task, result, listenableFutureExecutor );
84 * Returns the delegate ExecutorService.
86 protected ExecutorService getDelegate() {
91 public boolean awaitTermination( long timeout, TimeUnit unit ) throws InterruptedException {
92 return delegate.awaitTermination( timeout, unit );
96 public boolean isShutdown() {
97 return delegate.isShutdown();
101 public boolean isTerminated() {
102 return delegate.isTerminated();
106 public void shutdown() {
111 public List<Runnable> shutdownNow() {
112 return delegate.shutdownNow();
116 public void execute( Runnable command ) {
117 delegate.execute( command );
121 public <T> ListenableFuture<T> submit( Callable<T> task ) {
122 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task );
123 delegate.execute( futureTask );
128 public ListenableFuture<?> submit( Runnable task ) {
129 AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask( task, null );
130 delegate.execute( futureTask );
135 public <T> ListenableFuture<T> submit( Runnable task, T result ) {
136 AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task, result );
137 delegate.execute( futureTask );
141 protected ToStringHelper addToStringAttributes( ToStringHelper toStringHelper ) {
142 return toStringHelper;
146 public final String toString(){
147 return addToStringAttributes( Objects.toStringHelper( this )
148 .add( "delegate", delegate ) ).toString();