ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.9
Committed: Mon Jul 28 16:00:19 2003 UTC (20 years, 10 months ago) by tim
Branch: MAIN
Changes since 1.8: +17 -15 lines
Log Message:
Added addAll() back in.

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