CompletionService
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
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 I/O, 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
to actually execute the tasks, in which case the
CompletionService
only manages an internal completion
queue. The ExecutorCompletionService
class provides an
implementation of this approach.
Memory consistency effects: Actions in a thread prior to
submitting a task to a CompletionService
happen-before
actions taken by that task, which in turn happen-before
actions following a successful return from the corresponding take()
.
Public Method Summary
abstract
Future<V>
|
poll(long timeout, 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.
|
abstract
Future<V>
|
poll()
Retrieves and removes the Future representing the next
completed task, or null if none are present.
|
abstract
Future<V>
|
submit( Runnable task, V result)
Submits a Runnable task for execution and returns a Future
representing that task.
|
abstract
Future<V>
|
submit( Callable<V> task)
Submits a value-returning task for execution and returns a Future
representing the pending results of the task.
|
abstract
Future<V>
|
take()
Retrieves and removes the Future representing the next
completed task, waiting if none are yet present.
|
Public Methods
public
abstract
Future<V>
poll
(long timeout, 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.
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
public
abstract
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
public
abstract
Future<V>
submit
(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
public
abstract
Future<V>
submit
(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.
Returns
- a Future representing pending completion of the task
public
abstract
Future<V>
take
()
Retrieves and removes the Future representing the next
completed task, waiting if none are yet present.
Returns
- the Future representing the next completed task
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eCompletionService\u003c/code\u003e allows the submission of asynchronous tasks and retrieval of their results, decoupling production and consumption.\u003c/p\u003e\n"],["\u003cp\u003eIt manages a completion queue, often relying on a separate \u003ccode\u003eExecutor\u003c/code\u003e for task execution.\u003c/p\u003e\n"],["\u003cp\u003eConsumers can obtain completed tasks using \u003ccode\u003etake()\u003c/code\u003e (blocking), \u003ccode\u003epoll()\u003c/code\u003e (non-blocking), or \u003ccode\u003epoll(long, TimeUnit)\u003c/code\u003e (timed blocking).\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003esubmit()\u003c/code\u003e methods enable submitting tasks as either \u003ccode\u003eRunnable\u003c/code\u003e or \u003ccode\u003eCallable\u003c/code\u003e for execution.\u003c/p\u003e\n"],["\u003cp\u003eMemory consistency is guaranteed: actions before submission \u003cem\u003ehappen-before\u003c/em\u003e task actions, which \u003cem\u003ehappen-before\u003c/em\u003e actions after successful \u003ccode\u003etake()\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# CompletionService\n\npublic interface **CompletionService** \n\n|---|---|---|\n| Known Indirect Subclasses [ExecutorCompletionService](../../../../reference/java/util/concurrent/ExecutorCompletionService.html)\\\u003cV\\\u003e |-------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ExecutorCompletionService](../../../../reference/java/util/concurrent/ExecutorCompletionService.html)\\\u003cV\\\u003e | A [CompletionService](../../../../reference/java/util/concurrent/CompletionService.html) that uses a supplied [Executor](../../../../reference/java/util/concurrent/Executor.html) to execute tasks. | |||\n\nA service that decouples the production of new asynchronous tasks\nfrom the consumption of the results of completed tasks. Producers\n`submit` tasks for execution. Consumers `take`\ncompleted tasks and process their results in the order they\ncomplete. A `CompletionService` can for example be used to\nmanage asynchronous I/O, in which tasks that perform reads are\nsubmitted in one part of a program or system, and then acted upon\nin a different part of the program when the reads complete,\npossibly in a different order than they were requested.\n\nTypically, a `CompletionService` relies on a separate\n[Executor](../../../../reference/java/util/concurrent/Executor.html) to actually execute the tasks, in which case the\n`CompletionService` only manages an internal completion\nqueue. The [ExecutorCompletionService](../../../../reference/java/util/concurrent/ExecutorCompletionService.html) class provides an\nimplementation of this approach.\n\nMemory consistency effects: Actions in a thread prior to\nsubmitting a task to a `CompletionService`\n[*happen-before*](/j2objc/javadoc/jre/reference/java/util/concurrent/package-summary#MemoryVisibility)\nactions taken by that task, which in turn *happen-before*\nactions following a successful return from the corresponding `take()`. \n\n### Public Method Summary\n\n|--------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e | [poll](../../../../reference/java/util/concurrent/CompletionService.html#poll(long,%20java.util.concurrent.TimeUnit))(long timeout, [TimeUnit](../../../../reference/java/util/concurrent/TimeUnit.html) 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. |\n| abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e | [poll](../../../../reference/java/util/concurrent/CompletionService.html#poll())() Retrieves and removes the Future representing the next completed task, or `null` if none are present. |\n| abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e | [submit](../../../../reference/java/util/concurrent/CompletionService.html#submit(java.lang.Runnable,%20V))([Runnable](../../../../reference/java/lang/Runnable.html) task, V result) Submits a Runnable task for execution and returns a Future representing that task. |\n| abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e | [submit](../../../../reference/java/util/concurrent/CompletionService.html#submit(java.util.concurrent.Callable\u003cV\u003e))([Callable](../../../../reference/java/util/concurrent/Callable.html)\\\u003cV\\\u003e task) Submits a value-returning task for execution and returns a Future representing the pending results of the task. |\n| abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e | [take](../../../../reference/java/util/concurrent/CompletionService.html#take())() Retrieves and removes the Future representing the next completed task, waiting if none are yet present. |\n\nPublic Methods\n--------------\n\n#### public abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e\n**poll**\n(long timeout, [TimeUnit](../../../../reference/java/util/concurrent/TimeUnit.html) unit)\n\nRetrieves and removes the Future representing the next\ncompleted task, waiting if necessary up to the specified wait\ntime if none are yet present. \n\n##### Parameters\n\n| timeout | how long to wait before giving up, in units of `unit` |\n| unit | a `TimeUnit` determining how to interpret the `timeout` parameter |\n|---------|-------------------------------------------------------------------|\n\n##### Returns\n\n- the Future representing the next completed task or `null` if the specified waiting time elapses before one is present \n\n##### Throws\n\n| [InterruptedException](../../../../reference/java/lang/InterruptedException.html) | if interrupted while waiting |\n|-----------------------------------------------------------------------------------|------------------------------|\n\n#### public abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e\n**poll**\n()\n\nRetrieves and removes the Future representing the next\ncompleted task, or `null` if none are present. \n\n##### Returns\n\n- the Future representing the next completed task, or `null` if none are present \n\n#### public abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e\n**submit**\n([Runnable](../../../../reference/java/lang/Runnable.html) task, V result)\n\nSubmits a Runnable task for execution and returns a Future\nrepresenting that task. Upon completion, this task may be\ntaken or polled. \n\n##### Parameters\n\n| task | the task to submit |\n| result | the result to return upon successful completion |\n|--------|-------------------------------------------------|\n\n##### Returns\n\n- a Future representing pending completion of the task, and whose `get()` method will return the given result value upon completion \n\n##### Throws\n\n| [RejectedExecutionException](../../../../reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |\n| [NullPointerException](../../../../reference/java/lang/NullPointerException.html) | if the task is null |\n|----------------------------------------------------------------------------------------------------------|-----------------------------------------------|\n\n#### public abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e\n**submit**\n([Callable](../../../../reference/java/util/concurrent/Callable.html)\\\u003cV\\\u003e task)\n\nSubmits a value-returning task for execution and returns a Future\nrepresenting the pending results of the task. Upon completion,\nthis task may be taken or polled. \n\n##### Parameters\n\n| task | the task to submit |\n|------|--------------------|\n\n##### Returns\n\n- a Future representing pending completion of the task \n\n##### Throws\n\n| [RejectedExecutionException](../../../../reference/java/util/concurrent/RejectedExecutionException.html) | if the task cannot be scheduled for execution |\n| [NullPointerException](../../../../reference/java/lang/NullPointerException.html) | if the task is null |\n|----------------------------------------------------------------------------------------------------------|-----------------------------------------------|\n\n#### public abstract [Future](../../../../reference/java/util/concurrent/Future.html)\\\u003cV\\\u003e\n**take**\n()\n\nRetrieves and removes the Future representing the next\ncompleted task, waiting if none are yet present. \n\n##### Returns\n\n- the Future representing the next completed task \n\n##### Throws\n\n| [InterruptedException](../../../../reference/java/lang/InterruptedException.html) | if interrupted while waiting |\n|-----------------------------------------------------------------------------------|------------------------------|"]]