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