001 /*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.util.concurrent;
018
019 import com.google.common.annotations.Beta;
020
021 import java.util.concurrent.Executor;
022
023 /**
024 * <p>An abstract base implementation of the listener support provided by
025 * {@link ListenableFuture}. This class uses an {@link ExecutionList} to
026 * guarantee that all registered listeners will be executed. Listener/Executor
027 * pairs are stored in the execution list and executed in the order in which
028 * they were added, but because of thread scheduling issues there is no
029 * guarantee that the JVM will execute them in order. In addition, listeners
030 * added after the task is complete will be executed immediately, even if some
031 * previously added listeners have not yet been executed.
032 *
033 * <p>This class uses the {@link AbstractFuture} class to implement the
034 * {@code ListenableFuture} interface and simply delegates the
035 * {@link #addListener(Runnable, Executor)} and {@link #done()} methods to it.
036 *
037 * @author Sven Mawson
038 * @since 1
039 */
040 @Beta
041 public abstract class AbstractListenableFuture<V>
042 extends AbstractFuture<V> implements ListenableFuture<V> {
043
044 // The execution list to hold our executors.
045 private final ExecutionList executionList = new ExecutionList();
046
047 /*
048 * Adds a listener/executor pair to execution list to execute when this task
049 * is completed.
050 */
051 @Override
052 public void addListener(Runnable listener, Executor exec) {
053 executionList.add(listener, exec);
054 }
055
056 /*
057 * Override the done method to execute the execution list.
058 */
059 @Override
060 protected void done() {
061 executionList.run();
062 }
063 }