KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > CompletionService


1 /*
2  * @(#)CompletionService.java 1.1 04/01/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent;
9
10 /**
11  * A service that decouples the production of new asynchronous tasks
12  * from the consumption of the results of completed tasks. Producers
13  * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
14  * completed tasks and process their results in the order they
15  * complete. A <tt>CompletionService</tt> can for example be used to
16  * manage asynchronous IO, in which tasks that perform reads are
17  * submitted in one part of a program or system, and then acted upon
18  * in a different part of the program when the reads complete,
19  * possibly in a different order than they were requested.
20
21  * <p>
22  *
23  * Typically, a <tt>CompletionService</tt> relies on a separate {@link
24  * Executor} to actually execute the tasks, in which case the
25  * <tt>CompletionService</tt> only manages an internal completion
26  * queue. The {@link ExecutorCompletionService} class provides an
27  * implementation of this approach.
28  *
29  */

30 public interface CompletionService<V> {
31     /**
32      * Submits a value-returning task for execution and returns a Future
33      * representing the pending results of the task. Upon completion,
34      * this task may be taken or polled.
35      *
36      * @param task the task to submit
37      * @return a Future representing pending completion of the task
38      * @throws RejectedExecutionException if task cannot be scheduled
39      * for execution
40      * @throws NullPointerException if task null
41      */

42     Future JavaDoc<V> submit(Callable JavaDoc<V> task);
43
44
45     /**
46      * Submits a Runnable task for execution and returns a Future
47      * representing that task.Upon completion,
48      * this task may be taken or polled.
49      *
50      * @param task the task to submit
51      * @param result the result to return upon successful completion
52      * @return a Future representing pending completion of the task,
53      * and whose <tt>get()</tt> method will return the given result value
54      * upon completion
55      * @throws RejectedExecutionException if task cannot be scheduled
56      * for execution
57      * @throws NullPointerException if task null
58      */

59     Future JavaDoc<V> submit(Runnable JavaDoc task, V result);
60
61     /**
62      * Retrieves and removes the Future representing the next
63      * completed task, waiting if none are yet present.
64      * @return the Future representing the next completed task
65      * @throws InterruptedException if interrupted while waiting.
66      */

67     Future JavaDoc<V> take() throws InterruptedException JavaDoc;
68
69
70     /**
71      * Retrieves and removes the Future representing the next
72      * completed task or <tt>null</tt> if none are present.
73      *
74      * @return the Future representing the next completed task, or
75      * <tt>null</tt> if none are present.
76      */

77     Future JavaDoc<V> poll();
78
79     /**
80      * Retrieves and removes the Future representing the next
81      * completed task, waiting if necessary up to the specified wait
82      * time if none are yet present.
83      * @param timeout how long to wait before giving up, in units of
84      * <tt>unit</tt>
85      * @param unit a <tt>TimeUnit</tt> determining how to interpret the
86      * <tt>timeout</tt> parameter
87      * @return the Future representing the next completed task or
88      * <tt>null</tt> if the specified waiting time elapses before one
89      * is present.
90      * @throws InterruptedException if interrupted while waiting.
91      */

92     Future JavaDoc<V> poll(long timeout, TimeUnit JavaDoc unit) throws InterruptedException JavaDoc;
93 }
94
Popular Tags