001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.GwtIncompatible;
018import java.util.concurrent.Callable;
019import java.util.concurrent.Executor;
020import java.util.concurrent.FutureTask;
021import org.checkerframework.checker.nullness.compatqual.NullableDecl;
022
023/**
024 * A {@link FutureTask} that also implements the {@link ListenableFuture} interface. Unlike {@code
025 * FutureTask}, {@code ListenableFutureTask} does not provide an overrideable {@link
026 * FutureTask#done() done()} method. For similar functionality, call {@link #addListener}.
027 *
028 * <p>Few users should use this class. It is intended primarily for those who are implementing an
029 * {@code ExecutorService}. Most users should call {@link ListeningExecutorService#submit(Callable)
030 * ListeningExecutorService.submit} on a service obtained from {@link
031 * MoreExecutors#listeningDecorator}.
032 *
033 * @author Sven Mawson
034 * @since 1.0
035 */
036@GwtIncompatible
037public class ListenableFutureTask<V> extends FutureTask<V> implements ListenableFuture<V> {
038  // TODO(cpovirk): explore ways of making ListenableFutureTask final. There are some valid reasons
039  // such as BoundedQueueExecutorService to allow extends but it would be nice to make it final to
040  // avoid unintended usage.
041
042  // The execution list to hold our listeners.
043  private final ExecutionList executionList = new ExecutionList();
044
045  /**
046   * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code
047   * Callable}.
048   *
049   * @param callable the callable task
050   * @since 10.0
051   */
052  public static <V> ListenableFutureTask<V> create(Callable<V> callable) {
053    return new ListenableFutureTask<V>(callable);
054  }
055
056  /**
057   * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code
058   * Runnable}, and arrange that {@code get} will return the given result on successful completion.
059   *
060   * @param runnable the runnable task
061   * @param result the result to return on successful completion. If you don't need a particular
062   *     result, consider using constructions of the form: {@code ListenableFuture<?> f =
063   *     ListenableFutureTask.create(runnable, null)}
064   * @since 10.0
065   */
066  public static <V> ListenableFutureTask<V> create(Runnable runnable, @NullableDecl V result) {
067    return new ListenableFutureTask<V>(runnable, result);
068  }
069
070  ListenableFutureTask(Callable<V> callable) {
071    super(callable);
072  }
073
074  ListenableFutureTask(Runnable runnable, @NullableDecl V result) {
075    super(runnable, result);
076  }
077
078  @Override
079  public void addListener(Runnable listener, Executor exec) {
080    executionList.add(listener, exec);
081  }
082
083  /** Internal implementation detail used to invoke the listeners. */
084  @Override
085  protected void done() {
086    executionList.execute();
087  }
088}