java.util.concurrent
Interface CompletionService<V>

All Known Implementing Classes:
ExecutorCompletionService sample code for java.util.concurrent.ExecutorCompletionService definition code for java.util.concurrent.ExecutorCompletionService

public interface CompletionService<V>

A service that decouples the production of new asynchronous tasks from the consumption of the results of completed tasks. Producers submit tasks for execution. Consumers take completed tasks and process their results in the order they complete. A CompletionService can for example be used to manage asynchronous IO, in which tasks that perform reads are submitted in one part of a program or system, and then acted upon in a different part of the program when the reads complete, possibly in a different order than they were requested.

Typically, a CompletionService relies on a separate Executor sample code for java.util.concurrent.Executor definition code for java.util.concurrent.Executor to actually execute the tasks, in which case the CompletionService only manages an internal completion queue. The ExecutorCompletionService sample code for java.util.concurrent.ExecutorCompletionService definition code for java.util.concurrent.ExecutorCompletionService class provides an implementation of this approach.


Method Summary
 Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> poll sample code for java.util.concurrent.CompletionService.poll() definition code for java.util.concurrent.CompletionService.poll() ()
          Retrieves and removes the Future representing the next completed task or null if none are present.
 Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> poll sample code for java.util.concurrent.CompletionService.poll(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.CompletionService.poll(long, java.util.concurrent.TimeUnit) (long timeout, TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  unit)
          Retrieves and removes the Future representing the next completed task, waiting if necessary up to the specified wait time if none are yet present.
 Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> submit sample code for java.util.concurrent.CompletionService.submit(java.util.concurrent.Callable) definition code for java.util.concurrent.CompletionService.submit(java.util.concurrent.Callable) (Callable sample code for java.util.concurrent.Callable definition code for java.util.concurrent.Callable <V> task)
          Submits a value-returning task for execution and returns a Future representing the pending results of the task.
 Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> submit sample code for java.util.concurrent.CompletionService.submit(java.lang.Runnable, V) definition code for java.util.concurrent.CompletionService.submit(java.lang.Runnable, V) (Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable  task, V result)
          Submits a Runnable task for execution and returns a Future representing that task.Upon completion, this task may be taken or polled.
 Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> take sample code for java.util.concurrent.CompletionService.take() definition code for java.util.concurrent.CompletionService.take() ()
          Retrieves and removes the Future representing the next completed task, waiting if none are yet present.
 

Method Detail

submit sample code for java.util.concurrent.CompletionService.submit(java.util.concurrent.Callable<V>) definition code for java.util.concurrent.CompletionService.submit(java.util.concurrent.Callable<V>)

Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> submit(Callable sample code for java.util.concurrent.Callable definition code for java.util.concurrent.Callable <V> task)
Submits a value-returning task for execution and returns a Future representing the pending results of the task. Upon completion, this task may be taken or polled.

Parameters:
task - the task to submit
Returns:
a Future representing pending completion of the task
Throws:
RejectedExecutionException sample code for java.util.concurrent.RejectedExecutionException definition code for java.util.concurrent.RejectedExecutionException - if task cannot be scheduled for execution
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if task null

submit sample code for java.util.concurrent.CompletionService.submit(java.lang.Runnable, V) definition code for java.util.concurrent.CompletionService.submit(java.lang.Runnable, V)

Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> submit(Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable  task,
                 V result)
Submits a Runnable task for execution and returns a Future representing that task.Upon completion, this task may be taken or polled.

Parameters:
task - the task to submit
result - the result to return upon successful completion
Returns:
a Future representing pending completion of the task, and whose get() method will return the given result value upon completion
Throws:
RejectedExecutionException sample code for java.util.concurrent.RejectedExecutionException definition code for java.util.concurrent.RejectedExecutionException - if task cannot be scheduled for execution
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if task null

take sample code for java.util.concurrent.CompletionService.take() definition code for java.util.concurrent.CompletionService.take()

Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> take()
               throws InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException 
Retrieves and removes the Future representing the next completed task, waiting if none are yet present.

Returns:
the Future representing the next completed task
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if interrupted while waiting.

poll sample code for java.util.concurrent.CompletionService.poll() definition code for java.util.concurrent.CompletionService.poll()

Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> poll()
Retrieves and removes the Future representing the next completed task or null if none are present.

Returns:
the Future representing the next completed task, or null if none are present.

poll sample code for java.util.concurrent.CompletionService.poll(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.CompletionService.poll(long, java.util.concurrent.TimeUnit)

Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V> poll(long timeout,
               TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  unit)
               throws InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException 
Retrieves and removes the Future representing the next completed task, waiting if necessary up to the specified wait time if none are yet present.

Parameters:
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the Future representing the next completed task or null if the specified waiting time elapses before one is present.
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if interrupted while waiting.