024594d0f2c1b832ff06973a4f651725493c364a
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / AsyncNotifyingListeningExecutorService.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
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;
22
23 /**
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}.
36  *
37  * <p>
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.
42  *
43  * <p>
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.
49  *
50  * @author Thomas Pantelis
51  * @see AsyncNotifyingListenableFutureTask
52  */
53 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
54
55     private final ExecutorService delegate;
56     private final Executor listenableFutureExecutor;
57
58     /**
59      * Constructor.
60      *
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.
64      */
65     public AsyncNotifyingListeningExecutorService( final ExecutorService delegate,
66             @Nullable final Executor listenableFutureExecutor ) {
67         this.delegate = Preconditions.checkNotNull( delegate );
68         this.listenableFutureExecutor = listenableFutureExecutor;
69     }
70
71     /**
72      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
73      *
74      * @param task the Callable to execute
75      */
76     private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Callable<T> task ) {
77         return AsyncNotifyingListenableFutureTask.create( task, listenableFutureExecutor );
78     }
79
80     /**
81      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
82      *
83      * @param task the Runnable to execute
84      */
85     private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Runnable task, final T result ) {
86         return AsyncNotifyingListenableFutureTask.create( task, result, listenableFutureExecutor );
87     }
88
89     /**
90      * Returns the delegate ExecutorService.
91      */
92     protected ExecutorService getDelegate() {
93         return delegate;
94     }
95
96     @Override
97     public boolean awaitTermination( final long timeout, final TimeUnit unit ) throws InterruptedException {
98         return delegate.awaitTermination( timeout, unit );
99     }
100
101     @Override
102     public boolean isShutdown() {
103         return delegate.isShutdown();
104     }
105
106     @Override
107     public boolean isTerminated() {
108         return delegate.isTerminated();
109     }
110
111     @Override
112     public void shutdown() {
113         delegate.shutdown();
114     }
115
116     @Override
117     public List<Runnable> shutdownNow() {
118         return delegate.shutdownNow();
119     }
120
121     @Override
122     public void execute( final Runnable command ) {
123         delegate.execute( command );
124     }
125
126     @Override
127     public <T> ListenableFuture<T> submit( final Callable<T> task ) {
128         AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task );
129         delegate.execute( futureTask );
130         return futureTask;
131     }
132
133     @Override
134     public ListenableFuture<?> submit( final Runnable task ) {
135         AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask( task, null );
136         delegate.execute( futureTask );
137         return futureTask;
138     }
139
140     @Override
141     public <T> ListenableFuture<T> submit( final Runnable task, final T result ) {
142         AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task, result );
143         delegate.execute( futureTask );
144         return futureTask;
145     }
146
147     protected ToStringHelper addToStringAttributes( final ToStringHelper toStringHelper ) {
148         return toStringHelper;
149     }
150
151     @Override
152     public final String toString() {
153         return addToStringAttributes( MoreObjects.toStringHelper( this )
154                 .add( "delegate", delegate ) ).toString();
155     }
156 }