ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.21
Committed: Fri Sep 26 11:37:10 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
Spellcheck

File Contents

# User Rev Content
1 dl 1.2 /*
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 tim 1.1 package java.util.concurrent;
8 tim 1.9
9     import java.util.Collection;
10 tim 1.1 import java.util.Queue;
11    
12     /**
13 dl 1.4 * A {@link java.util.Queue} that additionally supports operations
14 brian 1.5 * that wait for elements to exist when retrieving them, and wait for
15 dholmes 1.10 * space to exist when storing them.
16 dl 1.2 *
17 dholmes 1.7 * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
18 dholmes 1.10 * Implementations throw <tt>NullPointerException</tt> on attempts
19 dl 1.2 * 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 dholmes 1.7 * <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 dl 1.2 * additional elements can be <tt>put</tt> without blocking.
26 dholmes 1.7 * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
27     * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
28 dl 1.2 *
29 dholmes 1.7 * <p> While <tt>BlockingQueue</tt> is designed to be used primarily
30 dl 1.18 * for producer-consumer queues, it additionally supports the {@link
31     * java.util.Collection} interface. So, for example, it is possible
32     * to remove an arbitrary element from a queue using
33 dl 1.6 * <tt>remove(x)</tt>. However, such operations are in general
34 dl 1.18 * <em>not</em> performed very efficiently, and are intended for only
35 dl 1.6 * occasional use, such as when a queued message is cancelled. Also,
36 dl 1.18 * the bulk operations, most notably <tt>addAll</tt> are <em>not</em>
37     * necessarily performed atomically, so it is possible for
38     * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
39     * only some of the elements in <tt>c</tt>.
40 dl 1.2 *
41 dholmes 1.7 * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
42 dl 1.2 * 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 brian 1.5 * such features tend to be implementation-dependent. For example, a
45 dl 1.2 * 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 tim 1.1 *
49     * <p>
50 brian 1.5 * Usage example, based on a typical producer-consumer scenario.
51 tim 1.9 * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
52 dholmes 1.7 * producers and multiple consumers.
53 tim 1.1 * <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 tim 1.15 * } catch (InterruptedException ex) { ... handle ...}
61 tim 1.1 * }
62     * Object produce() { ... }
63     * }
64     *
65     * class Consumer implements Runnable {
66     * private final BlockingQueue queue;
67 dl 1.21 * Consumer(BlockingQueue q) { queue = q; }
68 tim 1.1 * public void run() {
69     * try {
70     * while(true) { consume(queue.take()); }
71 tim 1.15 * } catch (InterruptedException ex) { ... handle ...}
72 tim 1.1 * }
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 brian 1.5 * Consumer c1 = new Consumer(q);
81     * Consumer c2 = new Consumer(q);
82 tim 1.1 * new Thread(p).start();
83 brian 1.5 * new Thread(c1).start();
84     * new Thread(c2).start();
85 tim 1.1 * }
86     * }
87     * </pre>
88     *
89     *
90     * @since 1.5
91 dl 1.6 * @author Doug Lea
92 tim 1.1 */
93     public interface BlockingQueue<E> extends Queue<E> {
94 dholmes 1.7
95     /**
96 dl 1.20 * Inserts the specified element into this queue, if possible. When
97 dl 1.18 * using queues that may impose insertion restrictions (for
98     * example capacity bounds), method <tt>offer</tt> is generally
99     * preferable to method {@link Collection#add}, which can fail to
100 dl 1.19 * insert an element only by throwing an exception.
101 dl 1.18 *
102 dl 1.19 * @param o the element to add.
103 dl 1.18 * @return <tt>true</tt> if it was possible to add the element to
104 dl 1.19 * this queue, else <tt>false</tt>
105     * @throws NullPointerException if the specified element is <tt>null</tt>
106 dholmes 1.7 */
107 tim 1.14 boolean offer(E o);
108 dl 1.20
109 tim 1.1 /**
110 dl 1.20 * Inserts the specified element into this queue, waiting if necessary
111 dl 1.18 * up to the specified wait time for space to become available.
112 dholmes 1.10 * @param o the element to add
113 dholmes 1.7 * @param timeout how long to wait before giving up, in units of
114     * <tt>unit</tt>
115 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
116 dholmes 1.7 * <tt>timeout</tt> parameter
117     * @return <tt>true</tt> if successful, or <tt>false</tt> if
118     * the specified waiting time elapses before space is available.
119 tim 1.1 * @throws InterruptedException if interrupted while waiting.
120 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>.
121 tim 1.1 */
122 dholmes 1.10 boolean offer(E o, long timeout, TimeUnit unit)
123 dholmes 1.7 throws InterruptedException;
124 tim 1.1
125     /**
126 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
127 dholmes 1.7 * if necessary up to the specified wait time if no elements are
128     * present on this queue.
129 dl 1.6 * @param timeout how long to wait before giving up, in units of
130     * <tt>unit</tt>
131 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
132 dholmes 1.7 * <tt>timeout</tt> parameter
133 tim 1.9 * @return the head of this queue, or <tt>null</tt> if the
134 dholmes 1.7 * specified waiting time elapses before an element is present.
135 tim 1.1 * @throws InterruptedException if interrupted while waiting.
136     */
137 tim 1.9 E poll(long timeout, TimeUnit unit)
138 tim 1.1 throws InterruptedException;
139    
140     /**
141 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
142 dholmes 1.7 * if no elements are present on this queue.
143     * @return the head of this queue
144 tim 1.1 * @throws InterruptedException if interrupted while waiting.
145     */
146 dholmes 1.7 E take() throws InterruptedException;
147    
148 tim 1.1 /**
149 dholmes 1.12 * Adds the specified element to this queue, waiting if necessary for
150 dholmes 1.7 * space to become available.
151 dholmes 1.10 * @param o the element to add
152 tim 1.1 * @throws InterruptedException if interrupted while waiting.
153 dholmes 1.13 * @throws NullPointerException if the specified element is <tt>null</tt>.
154 tim 1.1 */
155 dholmes 1.10 void put(E o) throws InterruptedException;
156 dholmes 1.7
157 dl 1.2 /**
158 dholmes 1.12 * Returns the number of elements that this queue can ideally (in
159 dl 1.2 * the absence of memory or resource constraints) accept without
160     * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
161 tim 1.9 * intrinsic limit.
162 dholmes 1.7 * <p>Note that you <em>cannot</em> always tell if
163 dl 1.2 * an attempt to <tt>add</tt> an element will succeed by
164     * inspecting <tt>remainingCapacity</tt> because it may be the
165     * case that a waiting consumer is ready to <tt>take</tt> an
166     * element out of an otherwise full queue.
167     * @return the remaining capacity
168 dl 1.6 */
169     int remainingCapacity();
170 tim 1.1
171 dl 1.18 /**
172     * Adds the specified element to this queue if it is possible to
173     * do so immediately, returning <tt>true</tt> upon success, else
174     * throwing an IllegalStateException.
175     * @param o the element
176     * @return <tt>true</tt> (as per the general contract of
177     * <tt>Collection.add</tt>).
178     *
179     * @throws NullPointerException if the specified element is <tt>null</tt>
180     * @throws IllegalStateException if element cannot be added
181     */
182     boolean add(E o);
183 dholmes 1.7
184 dl 1.18 /**
185     * Adds all of the elements in the specified collection to this
186     * queue if it is possible to do so. The behavior of this
187     * operation need not be atomic; a failure may occur after
188     * adding only some elements.
189     *
190     * @param c collection whose elements are to be added to this queue
191     * @return <tt>true</tt> if this queue changed as a result of the
192     * call.
193     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
194     * is <tt>null</tt>
195     * @throws IllegalStateException if any element cannot be added.
196     *
197     */
198     boolean addAll(Collection<? extends E> c);
199 dholmes 1.7
200 dl 1.18 }