1. /*
  2. * @(#)BlockingQueue.java 1.8 04/07/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. import java.util.Collection;
  9. import java.util.Queue;
  10. /**
  11. * A {@link java.util.Queue} that additionally supports operations
  12. * that wait for the queue to become non-empty when retrieving an element,
  13. * and wait for space to become available in the queue when storing an
  14. * element.
  15. *
  16. * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
  17. * Implementations throw <tt>NullPointerException</tt> on attempts
  18. * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A
  19. * <tt>null</tt> is used as a sentinel value to indicate failure of
  20. * <tt>poll</tt> operations.
  21. *
  22. * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
  23. * time it may have a <tt>remainingCapacity</tt> beyond which no
  24. * additional elements can be <tt>put</tt> without blocking.
  25. * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
  26. * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
  27. *
  28. * <p> <tt>BlockingQueue</tt> implementations are designed to be used
  29. * primarily for producer-consumer queues, but additionally support
  30. * the {@link java.util.Collection} interface. So, for example, it is
  31. * possible to remove an arbitrary element from a queue using
  32. * <tt>remove(x)</tt>. However, such operations are in general
  33. * <em>not</em> performed very efficiently, and are intended for only
  34. * occasional use, such as when a queued message is cancelled.
  35. *
  36. * <p> <tt>BlockingQueue</tt> implementations are thread-safe. All
  37. * queuing methods achieve their effects atomically using internal
  38. * locks or other forms of concurrency control. However, the
  39. * <em>bulk</em> Collection operations <tt>addAll</tt>,
  40. * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
  41. * <em>not</em> necessarily performed atomically unless specified
  42. * otherwise in an implementation. So it is possible, for example, for
  43. * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
  44. * only some of the elements in <tt>c</tt>.
  45. *
  46. * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
  47. * any kind of "close" or "shutdown" operation to
  48. * indicate that no more items will be added. The needs and usage of
  49. * such features tend to be implementation-dependent. For example, a
  50. * common tactic is for producers to insert special
  51. * <em>end-of-stream</em> or <em>poison</em> objects, that are
  52. * interpreted accordingly when taken by consumers.
  53. *
  54. * <p>
  55. * Usage example, based on a typical producer-consumer scenario.
  56. * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
  57. * producers and multiple consumers.
  58. * <pre>
  59. * class Producer implements Runnable {
  60. * private final BlockingQueue queue;
  61. * Producer(BlockingQueue q) { queue = q; }
  62. * public void run() {
  63. * try {
  64. * while(true) { queue.put(produce()); }
  65. * } catch (InterruptedException ex) { ... handle ...}
  66. * }
  67. * Object produce() { ... }
  68. * }
  69. *
  70. * class Consumer implements Runnable {
  71. * private final BlockingQueue queue;
  72. * Consumer(BlockingQueue q) { queue = q; }
  73. * public void run() {
  74. * try {
  75. * while(true) { consume(queue.take()); }
  76. * } catch (InterruptedException ex) { ... handle ...}
  77. * }
  78. * void consume(Object x) { ... }
  79. * }
  80. *
  81. * class Setup {
  82. * void main() {
  83. * BlockingQueue q = new SomeQueueImplementation();
  84. * Producer p = new Producer(q);
  85. * Consumer c1 = new Consumer(q);
  86. * Consumer c2 = new Consumer(q);
  87. * new Thread(p).start();
  88. * new Thread(c1).start();
  89. * new Thread(c2).start();
  90. * }
  91. * }
  92. * </pre>
  93. *
  94. * <p>This interface is a member of the
  95. * <a href="{@docRoot}/../guide/collections/index.html">
  96. * Java Collections Framework</a>.
  97. *
  98. * @since 1.5
  99. * @author Doug Lea
  100. * @param <E> the type of elements held in this collection
  101. */
  102. public interface BlockingQueue<E> extends Queue<E> {
  103. /**
  104. * Inserts the specified element into this queue, if possible. When
  105. * using queues that may impose insertion restrictions (for
  106. * example capacity bounds), method <tt>offer</tt> is generally
  107. * preferable to method {@link Collection#add}, which can fail to
  108. * insert an element only by throwing an exception.
  109. *
  110. * @param o the element to add.
  111. * @return <tt>true</tt> if it was possible to add the element to
  112. * this queue, else <tt>false</tt>
  113. * @throws NullPointerException if the specified element is <tt>null</tt>
  114. */
  115. boolean offer(E o);
  116. /**
  117. * Inserts the specified element into this queue, waiting if necessary
  118. * up to the specified wait time for space to become available.
  119. * @param o the element to add
  120. * @param timeout how long to wait before giving up, in units of
  121. * <tt>unit</tt>
  122. * @param unit a <tt>TimeUnit</tt> determining how to interpret the
  123. * <tt>timeout</tt> parameter
  124. * @return <tt>true</tt> if successful, or <tt>false</tt> if
  125. * the specified waiting time elapses before space is available.
  126. * @throws InterruptedException if interrupted while waiting.
  127. * @throws NullPointerException if the specified element is <tt>null</tt>.
  128. */
  129. boolean offer(E o, long timeout, TimeUnit unit)
  130. throws InterruptedException;
  131. /**
  132. * Retrieves and removes the head of this queue, waiting
  133. * if necessary up to the specified wait time if no elements are
  134. * present on this queue.
  135. * @param timeout how long to wait before giving up, in units of
  136. * <tt>unit</tt>
  137. * @param unit a <tt>TimeUnit</tt> determining how to interpret the
  138. * <tt>timeout</tt> parameter
  139. * @return the head of this queue, or <tt>null</tt> if the
  140. * specified waiting time elapses before an element is present.
  141. * @throws InterruptedException if interrupted while waiting.
  142. */
  143. E poll(long timeout, TimeUnit unit)
  144. throws InterruptedException;
  145. /**
  146. * Retrieves and removes the head of this queue, waiting
  147. * if no elements are present on this queue.
  148. * @return the head of this queue
  149. * @throws InterruptedException if interrupted while waiting.
  150. */
  151. E take() throws InterruptedException;
  152. /**
  153. * Adds the specified element to this queue, waiting if necessary for
  154. * space to become available.
  155. * @param o the element to add
  156. * @throws InterruptedException if interrupted while waiting.
  157. * @throws NullPointerException if the specified element is <tt>null</tt>.
  158. */
  159. void put(E o) throws InterruptedException;
  160. /**
  161. * Returns the number of elements that this queue can ideally (in
  162. * the absence of memory or resource constraints) accept without
  163. * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
  164. * intrinsic limit.
  165. * <p>Note that you <em>cannot</em> always tell if
  166. * an attempt to <tt>add</tt> an element will succeed by
  167. * inspecting <tt>remainingCapacity</tt> because it may be the
  168. * case that another thread is about to <tt>put</tt> or <tt>take</tt> an
  169. * element.
  170. * @return the remaining capacity
  171. */
  172. int remainingCapacity();
  173. /**
  174. * Adds the specified element to this queue if it is possible to
  175. * do so immediately, returning <tt>true</tt> upon success, else
  176. * throwing an IllegalStateException.
  177. * @param o the element
  178. * @return <tt>true</tt> (as per the general contract of
  179. * <tt>Collection.add</tt>).
  180. *
  181. * @throws NullPointerException if the specified element is <tt>null</tt>
  182. * @throws IllegalStateException if element cannot be added
  183. */
  184. boolean add(E o);
  185. /**
  186. * Removes all available elements from this queue and adds them
  187. * into the given collection. This operation may be more
  188. * efficient than repeatedly polling this queue. A failure
  189. * encountered while attempting to <tt>add</tt> elements to
  190. * collection <tt>c</tt> may result in elements being in neither,
  191. * either or both collections when the associated exception is
  192. * thrown. Attempts to drain a queue to itself result in
  193. * <tt>IllegalArgumentException</tt>. Further, the behavior of
  194. * this operation is undefined if the specified collection is
  195. * modified while the operation is in progress.
  196. *
  197. * @param c the collection to transfer elements into
  198. * @return the number of elements transferred.
  199. * @throws NullPointerException if c is null
  200. * @throws IllegalArgumentException if c is this queue
  201. *
  202. */
  203. int drainTo(Collection<? super E> c);
  204. /**
  205. * Removes at most the given number of available elements from
  206. * this queue and adds them into the given collection. A failure
  207. * encountered while attempting to <tt>add</tt> elements to
  208. * collection <tt>c</tt> may result in elements being in neither,
  209. * either or both collections when the associated exception is
  210. * thrown. Attempts to drain a queue to itself result in
  211. * <tt>IllegalArgumentException</tt>. Further, the behavior of
  212. * this operation is undefined if the specified collection is
  213. * modified while the operation is in progress.
  214. *
  215. * @param c the collection to transfer elements into
  216. * @param maxElements the maximum number of elements to transfer
  217. * @return the number of elements transferred.
  218. * @throws NullPointerException if c is null
  219. * @throws IllegalArgumentException if c is this queue
  220. */
  221. int drainTo(Collection<? super E> c, int maxElements);
  222. }