001/*
002 * Copyright (C) 2010 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 com.google.errorprone.annotations.DoNotMock;
019import java.util.Collection;
020import java.util.List;
021import java.util.concurrent.Callable;
022import java.util.concurrent.ExecutorService;
023import java.util.concurrent.Future;
024import java.util.concurrent.RejectedExecutionException;
025import java.util.concurrent.TimeUnit;
026
027/**
028 * An {@link ExecutorService} that returns {@link ListenableFuture} instances. To create an instance
029 * from an existing {@link ExecutorService}, call {@link
030 * MoreExecutors#listeningDecorator(ExecutorService)}.
031 *
032 * @author Chris Povirk
033 * @since 10.0
034 */
035@DoNotMock(
036    "Use TestingExecutors.sameThreadScheduledExecutor, or wrap a real Executor from "
037        + "java.util.concurrent.Executors with MoreExecutors.listeningDecorator")
038@GwtIncompatible
039public interface ListeningExecutorService extends ExecutorService {
040  /**
041   * @return a {@code ListenableFuture} representing pending completion of the task
042   * @throws RejectedExecutionException {@inheritDoc}
043   */
044  @Override
045  <T> ListenableFuture<T> submit(Callable<T> task);
046
047  /**
048   * @return a {@code ListenableFuture} representing pending completion of the task
049   * @throws RejectedExecutionException {@inheritDoc}
050   */
051  @Override
052  ListenableFuture<?> submit(Runnable task);
053
054  /**
055   * @return a {@code ListenableFuture} representing pending completion of the task
056   * @throws RejectedExecutionException {@inheritDoc}
057   */
058  @Override
059  <T> ListenableFuture<T> submit(Runnable task, T result);
060
061  /**
062   * {@inheritDoc}
063   *
064   * <p>All elements in the returned list must be {@link ListenableFuture} instances. The easiest
065   * way to obtain a {@code List<ListenableFuture<T>>} from this method is an unchecked (but safe)
066   * cast:
067   *
068   * <pre>
069   *   {@code @SuppressWarnings("unchecked") // guaranteed by invokeAll contract}
070   *   {@code List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks);}
071   * </pre>
072   *
073   * @return A list of {@code ListenableFuture} instances representing the tasks, in the same
074   *     sequential order as produced by the iterator for the given task list, each of which has
075   *     completed.
076   * @throws RejectedExecutionException {@inheritDoc}
077   * @throws NullPointerException if any task is null
078   */
079  @Override
080  <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
081      throws InterruptedException;
082
083  /**
084   * {@inheritDoc}
085   *
086   * <p>All elements in the returned list must be {@link ListenableFuture} instances. The easiest
087   * way to obtain a {@code List<ListenableFuture<T>>} from this method is an unchecked (but safe)
088   * cast:
089   *
090   * <pre>
091   *   {@code @SuppressWarnings("unchecked") // guaranteed by invokeAll contract}
092   *   {@code List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks, timeout, unit);}
093   * </pre>
094   *
095   * @return a list of {@code ListenableFuture} instances representing the tasks, in the same
096   *     sequential order as produced by the iterator for the given task list. If the operation did
097   *     not time out, each task will have completed. If it did time out, some of these tasks will
098   *     not have completed.
099   * @throws RejectedExecutionException {@inheritDoc}
100   * @throws NullPointerException if any task is null
101   */
102  @Override
103  <T> List<Future<T>> invokeAll(
104      Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
105      throws InterruptedException;
106}