ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingQueue.java
Revision: 1.14
Committed: Wed Aug 6 15:48:14 2003 UTC (20 years, 10 months ago) by tim
Branch: MAIN
CVS Tags: JSR166_CR1
Changes since 1.13: +3 -3 lines
Log Message:
Typo fix, minor style fix, and fix use of PBQ.offer(x, y, z) after removal
of throws IE

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 dholmes 1.10 * for producer-consumer queues, it additionally supports the
31 dl 1.6 * <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 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     * }
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 brian 1.5 * Consumer c1 = new Consumer(q);
83     * Consumer c2 = new Consumer(q);
84 tim 1.1 * new Thread(p).start();
85 brian 1.5 * new Thread(c1).start();
86     * new Thread(c2).start();
87 tim 1.1 * }
88     * }
89     * </pre>
90     *
91     *
92     * @since 1.5
93     * @spec JSR-166
94 tim 1.14 * @revised $Date: 2003/08/06 01:57:53 $
95 dholmes 1.11 * @editor $Author: dholmes $
96 dl 1.6 * @author Doug Lea
97 tim 1.1 */
98     public interface BlockingQueue<E> extends Queue<E> {
99 dholmes 1.7
100     /**
101     * @throws IllegalStateException if this queue is full
102 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>.
103 dholmes 1.7 */
104 dholmes 1.10 boolean add(E o);
105 dholmes 1.7
106 tim 1.9 /**
107     * @throws IllegalStateException if this queue is full
108 tim 1.14 * @throws NullPointerException if <tt>c</tt> or any element of <tt>c<tt>
109 dholmes 1.10 * is <tt>null</tt>.
110 tim 1.9 */
111     boolean addAll(Collection<? extends E> c);
112 dholmes 1.7
113 tim 1.9 /**
114 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>.
115 dholmes 1.7 */
116 tim 1.14 boolean offer(E o);
117 dholmes 1.7
118 tim 1.1 /**
119 dholmes 1.12 * Adds the specified element to this queue, waiting if necessary up to the
120 dholmes 1.7 * specified wait time for space to become available.
121 dholmes 1.10 * @param o the element to add
122 dholmes 1.7 * @param timeout how long to wait before giving up, in units of
123     * <tt>unit</tt>
124 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
125 dholmes 1.7 * <tt>timeout</tt> parameter
126     * @return <tt>true</tt> if successful, or <tt>false</tt> if
127     * the specified waiting time elapses before space is available.
128 tim 1.1 * @throws InterruptedException if interrupted while waiting.
129 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>.
130 tim 1.1 */
131 dholmes 1.10 boolean offer(E o, long timeout, TimeUnit unit)
132 dholmes 1.7 throws InterruptedException;
133 tim 1.1
134     /**
135 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
136 dholmes 1.7 * if necessary up to the specified wait time if no elements are
137     * present on this queue.
138 dl 1.6 * @param timeout how long to wait before giving up, in units of
139     * <tt>unit</tt>
140 tim 1.9 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
141 dholmes 1.7 * <tt>timeout</tt> parameter
142 tim 1.9 * @return the head of this queue, or <tt>null</tt> if the
143 dholmes 1.7 * specified waiting time elapses before an element is present.
144 tim 1.1 * @throws InterruptedException if interrupted while waiting.
145     */
146 tim 1.9 E poll(long timeout, TimeUnit unit)
147 tim 1.1 throws InterruptedException;
148    
149     /**
150 dholmes 1.12 * Retrieves and removes the head of this queue, waiting
151 dholmes 1.7 * if no elements are present on this queue.
152     * @return the head of this queue
153 tim 1.1 * @throws InterruptedException if interrupted while waiting.
154     */
155 dholmes 1.7 E take() throws InterruptedException;
156    
157 tim 1.1
158     /**
159 dholmes 1.12 * Adds the specified element to this queue, waiting if necessary for
160 dholmes 1.7 * space to become available.
161 dholmes 1.10 * @param o the element to add
162 tim 1.1 * @throws InterruptedException if interrupted while waiting.
163 dholmes 1.13 * @throws NullPointerException if the specified element is <tt>null</tt>.
164 tim 1.1 */
165 dholmes 1.10 void put(E o) throws InterruptedException;
166 dholmes 1.7
167 dl 1.2
168     /**
169 dholmes 1.12 * Returns the number of elements that this queue can ideally (in
170 dl 1.2 * the absence of memory or resource constraints) accept without
171     * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
172 tim 1.9 * intrinsic limit.
173 dholmes 1.7 * <p>Note that you <em>cannot</em> always tell if
174 dl 1.2 * an attempt to <tt>add</tt> an element will succeed by
175     * inspecting <tt>remainingCapacity</tt> because it may be the
176     * case that a waiting consumer is ready to <tt>take</tt> an
177     * element out of an otherwise full queue.
178     * @return the remaining capacity
179 dl 1.6 */
180     int remainingCapacity();
181 tim 1.1
182     }
183 dholmes 1.7
184    
185    
186    
187    
188    
189    
190