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. package java.util.concurrent;
  8. /**
  9. * A service that decouples the production of new asynchronous tasks
  10. * from the consumption of the results of completed tasks. Producers
  11. * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
  12. * completed tasks and process their results in the order they
  13. * complete. A <tt>CompletionService</tt> can for example be used to
  14. * manage asynchronous IO, in which tasks that perform reads are
  15. * submitted in one part of a program or system, and then acted upon
  16. * in a different part of the program when the reads complete,
  17. * possibly in a different order than they were requested.
  18. * <p>
  19. *
  20. * Typically, a <tt>CompletionService</tt> relies on a separate {@link
  21. * Executor} to actually execute the tasks, in which case the
  22. * <tt>CompletionService</tt> only manages an internal completion
  23. * queue. The {@link ExecutorCompletionService} class provides an
  24. * implementation of this approach.
  25. *
  26. */
  27. public interface CompletionService<V> {
  28. /**
  29. * Submits a value-returning task for execution and returns a Future
  30. * representing the pending results of the task. Upon completion,
  31. * this task may be taken or polled.
  32. *
  33. * @param task the task to submit
  34. * @return a Future representing pending completion of the task
  35. * @throws RejectedExecutionException if task cannot be scheduled
  36. * for execution
  37. * @throws NullPointerException if task null
  38. */
  39. Future<V> submit(Callable<V> task);
  40. /**
  41. * Submits a Runnable task for execution and returns a Future
  42. * representing that task.Upon completion,
  43. * this task may be taken or polled.
  44. *
  45. * @param task the task to submit
  46. * @param result the result to return upon successful completion
  47. * @return a Future representing pending completion of the task,
  48. * and whose <tt>get()</tt> method will return the given result value
  49. * upon completion
  50. * @throws RejectedExecutionException if task cannot be scheduled
  51. * for execution
  52. * @throws NullPointerException if task null
  53. */
  54. Future<V> submit(Runnable task, V result);
  55. /**
  56. * Retrieves and removes the Future representing the next
  57. * completed task, waiting if none are yet present.
  58. * @return the Future representing the next completed task
  59. * @throws InterruptedException if interrupted while waiting.
  60. */
  61. Future<V> take() throws InterruptedException;
  62. /**
  63. * Retrieves and removes the Future representing the next
  64. * completed task or <tt>null</tt> if none are present.
  65. *
  66. * @return the Future representing the next completed task, or
  67. * <tt>null</tt> if none are present.
  68. */
  69. Future<V> poll();
  70. /**
  71. * Retrieves and removes the Future representing the next
  72. * completed task, waiting if necessary up to the specified wait
  73. * time if none are yet present.
  74. * @param timeout how long to wait before giving up, in units of
  75. * <tt>unit</tt>
  76. * @param unit a <tt>TimeUnit</tt> determining how to interpret the
  77. * <tt>timeout</tt> parameter
  78. * @return the Future representing the next completed task or
  79. * <tt>null</tt> if the specified waiting time elapses before one
  80. * is present.
  81. * @throws InterruptedException if interrupted while waiting.
  82. */
  83. Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
  84. }