ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.7
Committed: Mon Jul 28 04:11:54 2003 UTC (20 years, 10 months ago) by dholmes
Branch: MAIN
Changes since 1.6: +70 -39 lines
Log Message:
Significant doc updates:
 - inherit comments where appropriate
 - ensure runtime exception comments inherited (overriding as needed)
 - consistent descriptions
 - introduce head and tail terminology

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain. Use, modify, and
4 * redistribute this code in any way without acknowledgement.
5 */
6
7 package java.util.concurrent;
8 import java.util.Queue;
9
10 /**
11 * A {@link java.util.Queue} that additionally supports operations
12 * that wait for elements to exist when retrieving them, and wait for
13 * space to exist when putting them.
14 *
15 * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
16 * Implementations throw <tt>IllegalArgumentException</tt> on attempts
17 * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A
18 * <tt>null</tt> is used as a sentinel value to indicate failure of
19 * <tt>poll</tt> operations.
20 *
21 * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
22 * time it may have a <tt>remainingCapacity</tt> beyond which no
23 * additional elements can be <tt>put</tt> without blocking.
24 * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
25 * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
26 *
27 * <p> While <tt>BlockingQueue</tt> is designed to be used primarily
28 * as for producer-consumer queues, it additionally supports the
29 * <tt>Collection</tt> interface. So, for example, it is possible to
30 * remove an arbitrary element from within a queue using
31 * <tt>remove(x)</tt>. However, such operations are in general
32 * <em>NOT</em> performed very efficiently, and are intended for only
33 * occasional use, such as when a queued message is cancelled. Also,
34 * the bulk operations, most notably <tt>addAll</tt> are <em>NOT</em>
35 * performed atomically, so it is possible for <tt>addAll(c)</tt> to
36 * fail (throwing an exception) after adding only some of the elements
37 * in <tt>c</tt>.
38 *
39 * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
40 * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
41 * indicate that no more items will be added. The needs and usage of
42 * such features tend to be implementation-dependent. For example, a
43 * common tactic is for producers to insert special
44 * <em>end-of-stream</em> or <em>poison</em> objects, that are
45 * interpreted accordingly when taken by consumers.
46 *
47 * <p>
48 * Usage example, based on a typical producer-consumer scenario.
49 * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
50 * producers and multiple consumers.
51 * <pre>
52 * class Producer implements Runnable {
53 * private final BlockingQueue queue;
54 * Producer(BlockingQueue q) { queue = q; }
55 * public void run() {
56 * try {
57 * while(true) { queue.put(produce()); }
58 * }
59 * catch (InterruptedException ex) { ... handle ...}
60 * }
61 * Object produce() { ... }
62 * }
63 *
64 * class Consumer implements Runnable {
65 * private final BlockingQueue queue;
66 * Concumer(BlockingQueue q) { queue = q; }
67 * public void run() {
68 * try {
69 * while(true) { consume(queue.take()); }
70 * }
71 * catch (InterruptedException ex) { ... handle ...}
72 * }
73 * void consume(Object x) { ... }
74 * }
75 *
76 * class Setup {
77 * void main() {
78 * BlockingQueue q = new SomeQueueImplementation();
79 * Producer p = new Producer(q);
80 * Consumer c1 = new Consumer(q);
81 * Consumer c2 = new Consumer(q);
82 * new Thread(p).start();
83 * new Thread(c1).start();
84 * new Thread(c2).start();
85 * }
86 * }
87 * </pre>
88 *
89 *
90 * @since 1.5
91 * @spec JSR-166
92 * @revised $Date: 2003/06/24 14:34:47 $
93 * @editor $Author: dl $
94 * @author Doug Lea
95 */
96 public interface BlockingQueue<E> extends Queue<E> {
97
98 /**
99 * @throws IllegalStateException if this queue is full
100 * @throws NullPointerException if <tt>x<tt> is <tt>null</tt>.
101 */
102 boolean add(E x);
103
104 /**
105 * @throws IllegalStateException if this queue is full
106 * @throws NullPointerException if <tt>x<tt> is <tt>null</tt>.
107 */
108 boolean addAll(Collection c);
109
110 /**
111 * @throws NullPointerException if <tt>x<tt> is <tt>null</tt>.
112 */
113 public boolean offer(E x);
114
115 /**
116 * Add the specified element to this queue, waiting if necessary up to the
117 * specified wait time for space to become available.
118 * @param x the element to add
119 * @param timeout how long to wait before giving up, in units of
120 * <tt>unit</tt>
121 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
122 * <tt>timeout</tt> parameter
123 * @return <tt>true</tt> if successful, or <tt>false</tt> if
124 * the specified waiting time elapses before space is available.
125 * @throws InterruptedException if interrupted while waiting.
126 * @throws NullPointerException if <tt>x</tt> is <tt>null</tt>.
127 */
128 boolean offer(E x, long timeout, TimeUnit unit)
129 throws InterruptedException;
130
131 /**
132 * Retrieve and remove 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 /**
147 * Retrieve and remove the head of this queue, waiting
148 * if no elements are present on this queue.
149 * @return the head of this queue
150 * @throws InterruptedException if interrupted while waiting.
151 */
152 E take() throws InterruptedException;
153
154
155 /**
156 * Add the specified element to this queue, waiting if necessary for
157 * space to become available.
158 * @param x the element to add
159 * @throws InterruptedException if interrupted while waiting.
160 * @throws NullPointerException if <tt>x</tt> is <tt>null</tt>.
161 */
162 void put(E x) throws InterruptedException;
163
164
165 /**
166 * Return the number of elements that this queue can ideally (in
167 * the absence of memory or resource constraints) accept without
168 * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
169 * intrinsic limit.
170 * <p>Note that you <em>cannot</em> always tell if
171 * an attempt to <tt>add</tt> an element will succeed by
172 * inspecting <tt>remainingCapacity</tt> because it may be the
173 * case that a waiting consumer is ready to <tt>take</tt> an
174 * element out of an otherwise full queue.
175 * @return the remaining capacity
176 */
177 int remainingCapacity();
178
179 }
180
181
182
183
184
185
186
187