Source Code Cross Referenced for BlockingQueue.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » concurrent » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003         *
004         * This code is free software; you can redistribute it and/or modify it
005         * under the terms of the GNU General Public License version 2 only, as
006         * published by the Free Software Foundation.  Sun designates this
007         * particular file as subject to the "Classpath" exception as provided
008         * by Sun in the LICENSE file that accompanied this code.
009         *
010         * This code is distributed in the hope that it will be useful, but WITHOUT
011         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
013         * version 2 for more details (a copy is included in the LICENSE file that
014         * accompanied this code).
015         *
016         * You should have received a copy of the GNU General Public License version
017         * 2 along with this work; if not, write to the Free Software Foundation,
018         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019         *
020         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021         * CA 95054 USA or visit www.sun.com if you need additional information or
022         * have any questions.
023         */
024
025        /*
026         * This file is available under and governed by the GNU General Public
027         * License version 2 only, as published by the Free Software Foundation.
028         * However, the following notice accompanied the original version of this
029         * file:
030         *
031         * Written by Doug Lea with assistance from members of JCP JSR-166
032         * Expert Group and released to the public domain, as explained at
033         * http://creativecommons.org/licenses/publicdomain
034         */
035
036        package java.util.concurrent;
037
038        import java.util.Collection;
039        import java.util.Queue;
040
041        /**
042         * A {@link java.util.Queue} that additionally supports operations
043         * that wait for the queue to become non-empty when retrieving an
044         * element, and wait for space to become available in the queue when
045         * storing an element.
046         *
047         * <p><tt>BlockingQueue</tt> methods come in four forms, with different ways
048         * of handling operations that cannot be satisfied immediately, but may be
049         * satisfied at some point in the future:
050         * one throws an exception, the second returns a special value (either
051         * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
052         * blocks the current thread indefinitely until the operation can succeed,
053         * and the fourth blocks for only a given maximum time limit before giving
054         * up.  These methods are summarized in the following table:
055         *
056         * <p>
057         * <table BORDER CELLPADDING=3 CELLSPACING=1>
058         *  <tr>
059         *    <td></td>
060         *    <td ALIGN=CENTER><em>Throws exception</em></td>
061         *    <td ALIGN=CENTER><em>Special value</em></td>
062         *    <td ALIGN=CENTER><em>Blocks</em></td>
063         *    <td ALIGN=CENTER><em>Times out</em></td>
064         *  </tr>
065         *  <tr>
066         *    <td><b>Insert</b></td>
067         *    <td>{@link #add add(e)}</td>
068         *    <td>{@link #offer offer(e)}</td>
069         *    <td>{@link #put put(e)}</td>
070         *    <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
071         *  </tr>
072         *  <tr>
073         *    <td><b>Remove</b></td>
074         *    <td>{@link #remove remove()}</td>
075         *    <td>{@link #poll poll()}</td>
076         *    <td>{@link #take take()}</td>
077         *    <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
078         *  </tr>
079         *  <tr>
080         *    <td><b>Examine</b></td>
081         *    <td>{@link #element element()}</td>
082         *    <td>{@link #peek peek()}</td>
083         *    <td><em>not applicable</em></td>
084         *    <td><em>not applicable</em></td>
085         *  </tr>
086         * </table>
087         *
088         * <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements.
089         * Implementations throw <tt>NullPointerException</tt> on attempts
090         * to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>.  A
091         * <tt>null</tt> is used as a sentinel value to indicate failure of
092         * <tt>poll</tt> operations.
093         *
094         * <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given
095         * time it may have a <tt>remainingCapacity</tt> beyond which no
096         * additional elements can be <tt>put</tt> without blocking.
097         * A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always
098         * reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>.
099         *
100         * <p> <tt>BlockingQueue</tt> implementations are designed to be used
101         * primarily for producer-consumer queues, but additionally support
102         * the {@link java.util.Collection} interface.  So, for example, it is
103         * possible to remove an arbitrary element from a queue using
104         * <tt>remove(x)</tt>. However, such operations are in general
105         * <em>not</em> performed very efficiently, and are intended for only
106         * occasional use, such as when a queued message is cancelled.
107         *
108         * <p> <tt>BlockingQueue</tt> implementations are thread-safe.  All
109         * queuing methods achieve their effects atomically using internal
110         * locks or other forms of concurrency control. However, the
111         * <em>bulk</em> Collection operations <tt>addAll</tt>,
112         * <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are
113         * <em>not</em> necessarily performed atomically unless specified
114         * otherwise in an implementation. So it is possible, for example, for
115         * <tt>addAll(c)</tt> to fail (throwing an exception) after adding
116         * only some of the elements in <tt>c</tt>.
117         *
118         * <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support
119         * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
120         * indicate that no more items will be added.  The needs and usage of
121         * such features tend to be implementation-dependent. For example, a
122         * common tactic is for producers to insert special
123         * <em>end-of-stream</em> or <em>poison</em> objects, that are
124         * interpreted accordingly when taken by consumers.
125         *
126         * <p>
127         * Usage example, based on a typical producer-consumer scenario.
128         * Note that a <tt>BlockingQueue</tt> can safely be used with multiple
129         * producers and multiple consumers.
130         * <pre>
131         * class Producer implements Runnable {
132         *   private final BlockingQueue queue;
133         *   Producer(BlockingQueue q) { queue = q; }
134         *   public void run() {
135         *     try {
136         *       while (true) { queue.put(produce()); }
137         *     } catch (InterruptedException ex) { ... handle ...}
138         *   }
139         *   Object produce() { ... }
140         * }
141         *
142         * class Consumer implements Runnable {
143         *   private final BlockingQueue queue;
144         *   Consumer(BlockingQueue q) { queue = q; }
145         *   public void run() {
146         *     try {
147         *       while (true) { consume(queue.take()); }
148         *     } catch (InterruptedException ex) { ... handle ...}
149         *   }
150         *   void consume(Object x) { ... }
151         * }
152         *
153         * class Setup {
154         *   void main() {
155         *     BlockingQueue q = new SomeQueueImplementation();
156         *     Producer p = new Producer(q);
157         *     Consumer c1 = new Consumer(q);
158         *     Consumer c2 = new Consumer(q);
159         *     new Thread(p).start();
160         *     new Thread(c1).start();
161         *     new Thread(c2).start();
162         *   }
163         * }
164         * </pre>
165         *
166         * <p>Memory consistency effects: As with other concurrent
167         * collections, actions in a thread prior to placing an object into a
168         * {@code BlockingQueue}
169         * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
170         * actions subsequent to the access or removal of that element from
171         * the {@code BlockingQueue} in another thread.
172         *
173         * <p>This interface is a member of the
174         * <a href="{@docRoot}/../technotes/guides/collections/index.html">
175         * Java Collections Framework</a>.
176         *
177         * @since 1.5
178         * @author Doug Lea
179         * @param <E> the type of elements held in this collection
180         */
181        public interface BlockingQueue<E> extends Queue<E> {
182            /**
183             * Inserts the specified element into this queue if it is possible to do
184             * so immediately without violating capacity restrictions, returning
185             * <tt>true</tt> upon success and throwing an
186             * <tt>IllegalStateException</tt> if no space is currently available.
187             * When using a capacity-restricted queue, it is generally preferable to
188             * use {@link #offer(Object) offer}.
189             *
190             * @param e the element to add
191             * @return <tt>true</tt> (as specified by {@link Collection#add})
192             * @throws IllegalStateException if the element cannot be added at this
193             *         time due to capacity restrictions
194             * @throws ClassCastException if the class of the specified element
195             *         prevents it from being added to this queue
196             * @throws NullPointerException if the specified element is null
197             * @throws IllegalArgumentException if some property of the specified
198             *         element prevents it from being added to this queue
199             */
200            boolean add(E e);
201
202            /**
203             * Inserts the specified element into this queue if it is possible to do
204             * so immediately without violating capacity restrictions, returning
205             * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
206             * available.  When using a capacity-restricted queue, this method is
207             * generally preferable to {@link #add}, which can fail to insert an
208             * element only by throwing an exception.
209             *
210             * @param e the element to add
211             * @return <tt>true</tt> if the element was added to this queue, else
212             *         <tt>false</tt>
213             * @throws ClassCastException if the class of the specified element
214             *         prevents it from being added to this queue
215             * @throws NullPointerException if the specified element is null
216             * @throws IllegalArgumentException if some property of the specified
217             *         element prevents it from being added to this queue
218             */
219            boolean offer(E e);
220
221            /**
222             * Inserts the specified element into this queue, waiting if necessary
223             * for space to become available.
224             *
225             * @param e the element to add
226             * @throws InterruptedException if interrupted while waiting
227             * @throws ClassCastException if the class of the specified element
228             *         prevents it from being added to this queue
229             * @throws NullPointerException if the specified element is null
230             * @throws IllegalArgumentException if some property of the specified
231             *         element prevents it from being added to this queue
232             */
233            void put(E e) throws InterruptedException;
234
235            /**
236             * Inserts the specified element into this queue, waiting up to the
237             * specified wait time if necessary for space to become available.
238             *
239             * @param e the element to add
240             * @param timeout how long to wait before giving up, in units of
241             *        <tt>unit</tt>
242             * @param unit a <tt>TimeUnit</tt> determining how to interpret the
243             *        <tt>timeout</tt> parameter
244             * @return <tt>true</tt> if successful, or <tt>false</tt> if
245             *         the specified waiting time elapses before space is available
246             * @throws InterruptedException if interrupted while waiting
247             * @throws ClassCastException if the class of the specified element
248             *         prevents it from being added to this queue
249             * @throws NullPointerException if the specified element is null
250             * @throws IllegalArgumentException if some property of the specified
251             *         element prevents it from being added to this queue
252             */
253            boolean offer(E e, long timeout, TimeUnit unit)
254                    throws InterruptedException;
255
256            /**
257             * Retrieves and removes the head of this queue, waiting if necessary
258             * until an element becomes available.
259             *
260             * @return the head of this queue
261             * @throws InterruptedException if interrupted while waiting
262             */
263            E take() throws InterruptedException;
264
265            /**
266             * Retrieves and removes the head of this queue, waiting up to the
267             * specified wait time if necessary for an element to become available.
268             *
269             * @param timeout how long to wait before giving up, in units of
270             *        <tt>unit</tt>
271             * @param unit a <tt>TimeUnit</tt> determining how to interpret the
272             *        <tt>timeout</tt> parameter
273             * @return the head of this queue, or <tt>null</tt> if the
274             *         specified waiting time elapses before an element is available
275             * @throws InterruptedException if interrupted while waiting
276             */
277            E poll(long timeout, TimeUnit unit) throws InterruptedException;
278
279            /**
280             * Returns the number of additional elements that this queue can ideally
281             * (in the absence of memory or resource constraints) accept without
282             * blocking, or <tt>Integer.MAX_VALUE</tt> if there is no intrinsic
283             * limit.
284             *
285             * <p>Note that you <em>cannot</em> always tell if an attempt to insert
286             * an element will succeed by inspecting <tt>remainingCapacity</tt>
287             * because it may be the case that another thread is about to
288             * insert or remove an element.
289             *
290             * @return the remaining capacity
291             */
292            int remainingCapacity();
293
294            /**
295             * Removes a single instance of the specified element from this queue,
296             * if it is present.  More formally, removes an element <tt>e</tt> such
297             * that <tt>o.equals(e)</tt>, if this queue contains one or more such
298             * elements.
299             * Returns <tt>true</tt> if this queue contained the specified element
300             * (or equivalently, if this queue changed as a result of the call).
301             *
302             * @param o element to be removed from this queue, if present
303             * @return <tt>true</tt> if this queue changed as a result of the call
304             * @throws ClassCastException if the class of the specified element
305             *         is incompatible with this queue (optional)
306             * @throws NullPointerException if the specified element is null (optional)
307             */
308            boolean remove(Object o);
309
310            /**
311             * Returns <tt>true</tt> if this queue contains the specified element.
312             * More formally, returns <tt>true</tt> if and only if this queue contains
313             * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
314             *
315             * @param o object to be checked for containment in this queue
316             * @return <tt>true</tt> if this queue contains the specified element
317             * @throws ClassCastException if the class of the specified element
318             *         is incompatible with this queue (optional)
319             * @throws NullPointerException if the specified element is null (optional)
320             */
321            public boolean contains(Object o);
322
323            /**
324             * Removes all available elements from this queue and adds them
325             * to the given collection.  This operation may be more
326             * efficient than repeatedly polling this queue.  A failure
327             * encountered while attempting to add elements to
328             * collection <tt>c</tt> may result in elements being in neither,
329             * either or both collections when the associated exception is
330             * thrown.  Attempts to drain a queue to itself result in
331             * <tt>IllegalArgumentException</tt>. Further, the behavior of
332             * this operation is undefined if the specified collection is
333             * modified while the operation is in progress.
334             *
335             * @param c the collection to transfer elements into
336             * @return the number of elements transferred
337             * @throws UnsupportedOperationException if addition of elements
338             *         is not supported by the specified collection
339             * @throws ClassCastException if the class of an element of this queue
340             *         prevents it from being added to the specified collection
341             * @throws NullPointerException if the specified collection is null
342             * @throws IllegalArgumentException if the specified collection is this
343             *         queue, or some property of an element of this queue prevents
344             *         it from being added to the specified collection
345             */
346            int drainTo(Collection<? super  E> c);
347
348            /**
349             * Removes at most the given number of available elements from
350             * this queue and adds them to the given collection.  A failure
351             * encountered while attempting to add elements to
352             * collection <tt>c</tt> may result in elements being in neither,
353             * either or both collections when the associated exception is
354             * thrown.  Attempts to drain a queue to itself result in
355             * <tt>IllegalArgumentException</tt>. Further, the behavior of
356             * this operation is undefined if the specified collection is
357             * modified while the operation is in progress.
358             *
359             * @param c the collection to transfer elements into
360             * @param maxElements the maximum number of elements to transfer
361             * @return the number of elements transferred
362             * @throws UnsupportedOperationException if addition of elements
363             *         is not supported by the specified collection
364             * @throws ClassCastException if the class of an element of this queue
365             *         prevents it from being added to the specified collection
366             * @throws NullPointerException if the specified collection is null
367             * @throws IllegalArgumentException if the specified collection is this
368             *         queue, or some property of an element of this queue prevents
369             *         it from being added to the specified collection
370             */
371            int drainTo(Collection<? super  E> c, int maxElements);
372        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.