Interface ListeningExecutorService

    • Method Detail

      • submit

        <T> ListenableFuture<T> submit​(Callable<T> task)
        Description copied from interface: java.util.concurrent.ExecutorService
        Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's get method will return the task's result upon successful completion.

        If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();

        Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted.

        Specified by:
        submit in interface ExecutorService
        Type Parameters:
        T - the type of the task's result
        Parameters:
        task - the task to submit
        Returns:
        a ListenableFuture representing pending completion of the task
        Throws:
        RejectedExecutionException - if the task cannot be scheduled for execution
      • submit

        <T> ListenableFuture<T> submit​(Runnable task,
                                       T result)
        Description copied from interface: java.util.concurrent.ExecutorService
        Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion.
        Specified by:
        submit in interface ExecutorService
        Type Parameters:
        T - the type of the result
        Parameters:
        task - the task to submit
        result - the result to return
        Returns:
        a ListenableFuture representing pending completion of the task
        Throws:
        RejectedExecutionException - if the task cannot be scheduled for execution
      • invokeAll

        <T> List<Future<T>> invokeAll​(Collection<? extends Callable<T>> tasks)
                               throws InterruptedException
        Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

        All elements in the returned list must be ListenableFuture instances. The easiest way to obtain a List<ListenableFuture<T>> from this method is an unchecked (but safe) cast:

           @SuppressWarnings("unchecked") // guaranteed by invokeAll contract
           List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks);
         
        Specified by:
        invokeAll in interface ExecutorService
        Type Parameters:
        T - the type of the values returned from the tasks
        Parameters:
        tasks - the collection of tasks
        Returns:
        A list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed.
        Throws:
        RejectedExecutionException - if any task cannot be scheduled for execution
        NullPointerException - if any task is null
        InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelled
      • invokeAll

        <T> List<Future<T>> invokeAll​(Collection<? extends Callable<T>> tasks,
                                      long timeout,
                                      TimeUnit unit)
                               throws InterruptedException
        Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future.isDone() is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

        All elements in the returned list must be ListenableFuture instances. The easiest way to obtain a List<ListenableFuture<T>> from this method is an unchecked (but safe) cast:

           @SuppressWarnings("unchecked") // guaranteed by invokeAll contract
           List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks, timeout, unit);
         
        Specified by:
        invokeAll in interface ExecutorService
        Type Parameters:
        T - the type of the values returned from the tasks
        Parameters:
        tasks - the collection of tasks
        timeout - the maximum time to wait
        unit - the time unit of the timeout argument
        Returns:
        a list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
        Throws:
        RejectedExecutionException - if any task cannot be scheduled for execution
        NullPointerException - if any task is null
        InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelled