KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > BlockingQueue


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
8 package java.util.concurrent;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Queue JavaDoc;
12
13 /**
14  * A {@link java.util.Queue} that additionally supports operations
15  * that wait for the queue to become non-empty when retrieving an element,
16  * and wait for space to become available in the queue when storing an
17  * element.
18  *
19  * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
20  * Implementations throw <tt>NullPointerException</tt> on attempts
21  * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A
22  * <tt>null</tt> is used as a sentinel value to indicate failure of
23  * <tt>poll</tt> operations.
24  *
25  * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
26  * time it may have a <tt>remainingCapacity</tt> beyond which no
27  * additional elements can be <tt>put</tt> without blocking.
28  * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
29  * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
30  *
31  * <p> <tt>BlockingQueue</tt> implementations are designed to be used
32  * primarily for producer-consumer queues, but additionally support
33  * the {@link java.util.Collection} interface. So, for example, it is
34  * possible to remove an arbitrary element from a queue using
35  * <tt>remove(x)</tt>. However, such operations are in general
36  * <em>not</em> performed very efficiently, and are intended for only
37  * occasional use, such as when a queued message is cancelled.
38  *
39  * <p> <tt>BlockingQueue</tt> implementations are thread-safe. All
40  * queuing methods achieve their effects atomically using internal
41  * locks or other forms of concurrency control. However, the
42  * <em>bulk</em> Collection operations <tt>addAll</tt>,
43  * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
44  * <em>not</em> necessarily performed atomically unless specified
45  * otherwise in an implementation. So it is possible, for example, for
46  * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
47  * only some of the elements in <tt>c</tt>.
48  *
49  * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
50  * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
51  * indicate that no more items will be added. The needs and usage of
52  * such features tend to be implementation-dependent. For example, a
53  * common tactic is for producers to insert special
54  * <em>end-of-stream</em> or <em>poison</em> objects, that are
55  * interpreted accordingly when taken by consumers.
56  *
57  * <p>
58  * Usage example, based on a typical producer-consumer scenario.
59  * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
60  * producers and multiple consumers.
61  * <pre>
62  * class Producer implements Runnable {
63  * private final BlockingQueue queue;
64  * Producer(BlockingQueue q) { queue = q; }
65  * public void run() {
66  * try {
67  * while(true) { queue.put(produce()); }
68  * } catch (InterruptedException ex) { ... handle ...}
69  * }
70  * Object produce() { ... }
71  * }
72  *
73  * class Consumer implements Runnable {
74  * private final BlockingQueue queue;
75  * Consumer(BlockingQueue q) { queue = q; }
76  * public void run() {
77  * try {
78  * while(true) { consume(queue.take()); }
79  * } catch (InterruptedException ex) { ... handle ...}
80  * }
81  * void consume(Object x) { ... }
82  * }
83  *
84  * class Setup {
85  * void main() {
86  * BlockingQueue q = new SomeQueueImplementation();
87  * Producer p = new Producer(q);
88  * Consumer c1 = new Consumer(q);
89  * Consumer c2 = new Consumer(q);
90  * new Thread(p).start();
91  * new Thread(c1).start();
92  * new Thread(c2).start();
93  * }
94  * }
95  * </pre>
96  *
97  * <p>This interface is a member of the
98  * <a HREF="{@docRoot}/../guide/collections/index.html">
99  * Java Collections Framework</a>.
100  *
101  * @since 1.5
102  * @author Doug Lea
103  * @param <E> the type of elements held in this collection
104  */

105 public interface BlockingQueue<E> extends Queue JavaDoc<E> {
106
107     /**
108      * Inserts the specified element into this queue, if possible. When
109      * using queues that may impose insertion restrictions (for
110      * example capacity bounds), method <tt>offer</tt> is generally
111      * preferable to method {@link Collection#add}, which can fail to
112      * insert an element only by throwing an exception.
113      *
114      * @param o the element to add.
115      * @return <tt>true</tt> if it was possible to add the element to
116      * this queue, else <tt>false</tt>
117      * @throws NullPointerException if the specified element is <tt>null</tt>
118      */

119     boolean offer(E o);
120     
121     /**
122      * Inserts the specified element into this queue, waiting if necessary
123      * up to the specified wait time for space to become available.
124      * @param o the element to add
125      * @param timeout how long to wait before giving up, in units of
126      * <tt>unit</tt>
127      * @param unit a <tt>TimeUnit</tt> determining how to interpret the
128      * <tt>timeout</tt> parameter
129      * @return <tt>true</tt> if successful, or <tt>false</tt> if
130      * the specified waiting time elapses before space is available.
131      * @throws InterruptedException if interrupted while waiting.
132      * @throws NullPointerException if the specified element is <tt>null</tt>.
133      */

134     boolean offer(E o, long timeout, TimeUnit JavaDoc unit)
135         throws InterruptedException JavaDoc;
136
137     /**
138      * Retrieves and removes the head of this queue, waiting
139      * if necessary up to the specified wait time if no elements are
140      * present on this queue.
141      * @param timeout how long to wait before giving up, in units of
142      * <tt>unit</tt>
143      * @param unit a <tt>TimeUnit</tt> determining how to interpret the
144      * <tt>timeout</tt> parameter
145      * @return the head of this queue, or <tt>null</tt> if the
146      * specified waiting time elapses before an element is present.
147      * @throws InterruptedException if interrupted while waiting.
148      */

149     E poll(long timeout, TimeUnit JavaDoc unit)
150         throws InterruptedException JavaDoc;
151
152     /**
153      * Retrieves and removes the head of this queue, waiting
154      * if no elements are present on this queue.
155      * @return the head of this queue
156      * @throws InterruptedException if interrupted while waiting.
157      */

158     E take() throws InterruptedException JavaDoc;
159
160     /**
161      * Adds the specified element to this queue, waiting if necessary for
162      * space to become available.
163      * @param o the element to add
164      * @throws InterruptedException if interrupted while waiting.
165      * @throws NullPointerException if the specified element is <tt>null</tt>.
166      */

167     void put(E o) throws InterruptedException JavaDoc;
168
169     /**
170      * Returns the number of elements that this queue can ideally (in
171      * the absence of memory or resource constraints) accept without
172      * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no
173      * intrinsic limit.
174      * <p>Note that you <em>cannot</em> always tell if
175      * an attempt to <tt>add</tt> an element will succeed by
176      * inspecting <tt>remainingCapacity</tt> because it may be the
177      * case that another thread is about to <tt>put</tt> or <tt>take</tt> an
178      * element.
179      * @return the remaining capacity
180      */

181     int remainingCapacity();
182
183     /**
184      * Adds the specified element to this queue if it is possible to
185      * do so immediately, returning <tt>true</tt> upon success, else
186      * throwing an IllegalStateException.
187      * @param o the element
188      * @return <tt>true</tt> (as per the general contract of
189      * <tt>Collection.add</tt>).
190      *
191      * @throws NullPointerException if the specified element is <tt>null</tt>
192      * @throws IllegalStateException if element cannot be added
193      */

194     boolean add(E o);
195
196     /**
197      * Removes all available elements from this queue and adds them
198      * into the given collection. This operation may be more
199      * efficient than repeatedly polling this queue. A failure
200      * encountered while attempting to <tt>add</tt> elements to
201      * collection <tt>c</tt> may result in elements being in neither,
202      * either or both collections when the associated exception is
203      * thrown. Attempts to drain a queue to itself result in
204      * <tt>IllegalArgumentException</tt>. Further, the behavior of
205      * this operation is undefined if the specified collection is
206      * modified while the operation is in progress.
207      *
208      * @param c the collection to transfer elements into
209      * @return the number of elements transferred.
210      * @throws NullPointerException if c is null
211      * @throws IllegalArgumentException if c is this queue
212      *
213      */

214     int drainTo(Collection JavaDoc<? super E> c);
215     
216     /**
217      * Removes at most the given number of available elements from
218      * this queue and adds them into the given collection. A failure
219      * encountered while attempting to <tt>add</tt> elements to
220      * collection <tt>c</tt> may result in elements being in neither,
221      * either or both collections when the associated exception is
222      * thrown. Attempts to drain a queue to itself result in
223      * <tt>IllegalArgumentException</tt>. Further, the behavior of
224      * this operation is undefined if the specified collection is
225      * modified while the operation is in progress.
226      *
227      * @param c the collection to transfer elements into
228      * @param maxElements the maximum number of elements to transfer
229      * @return the number of elements transferred.
230      * @throws NullPointerException if c is null
231      * @throws IllegalArgumentException if c is this queue
232      */

233     int drainTo(Collection JavaDoc<? super E> c, int maxElements);
234 }
235
Popular Tags