KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > BlockingQueue


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: BlockingQueue.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/collection/BlockingQueue.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:09 $
47  */

48  
49 package com.sun.enterprise.util.collection;
50
51 import java.util.LinkedList JavaDoc;
52 import java.util.Collection JavaDoc;
53
54 import com.sun.enterprise.util.pool.TimedoutException;
55 //Bug 4677074 begin
56
import java.util.logging.Logger JavaDoc;
57 import java.util.logging.Level JavaDoc;
58 import com.sun.logging.LogDomains;
59 //Bug 4677074 end
60

61 /**
62  * A BlockingQueue is a queue where remove() blocks if the queue is empty. The thread calling
63  * remove() blocks if the queue is empty while the add() notifies any waiting thread.
64  */

65  
66  /*
67   * NOTE:- Make sure that you synchronize the entire object on critical sections. For example,
68   * DO NOT try to synchronize just the access to the lnked list, because the FastThreadPool
69   *
70   */

71 public class BlockingQueue {
72  
73 //Bug 4677074 begin
74
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
75 //Bug 4677074 end
76
private boolean closed = false;
77     private boolean aborted = false;
78     private int limit;
79     private LinkedList JavaDoc list;
80         private int waiters=0; // added for 4682740
81

82     /**
83      * Create a BlockingQueue that has an infinite queuelength with the
84      * specified timeout.
85      * @param The maximum time remove() will block.
86      * @see remove()
87      */

88     public BlockingQueue() {
89         this(Integer.MAX_VALUE);
90     }
91     
92     /**
93      * Create a BlockingQueue that has the specified queue limit with the
94      * specified timeout.
95      * @param The maximum time remove() will block.
96      * @param The queue length after which TooManyTasksException is thrown.
97      * @see remove()
98      */

99     public BlockingQueue(int queueLimit) {
100         this.limit = queueLimit;
101         this.list = new LinkedList JavaDoc();
102                 // START OF IASRI 4682740
103
com.sun.enterprise.util.MonitorTask.addORBMonitorable(this);
104                 // END OF IASRI 4682740
105
}
106     
107     
108     /**
109      * Add to the head of the queue. Probably a high priority job?
110      */

111     public void addFirst(Object JavaDoc object)
112         throws TooManyTasksException, QueueClosedException
113     {
114         if (closed)
115             throw new QueueClosedException("Queue closed.");
116         synchronized (list) {
117             if (list.size() >= limit) {
118                 throw new TooManyTasksException("Too many tasks in queue...");
119             }
120             list.addFirst(object);
121             list.notify();
122         }
123     }
124     
125     /**
126      * Add to the tail of the queue.
127      */

128     public void addLast(Object JavaDoc object)
129         throws TooManyTasksException, QueueClosedException
130     {
131         if (closed)
132             throw new QueueClosedException("Queue closed.");
133         synchronized (list) {
134             if (list.size() >= limit) {
135                 throw new TooManyTasksException("Too many tasks in queue...");
136             }
137             list.add(object);
138             list.notify();
139         }
140     }
141     
142     /**
143      * Add the job at the specified position. Probably based on priority?
144      */

145     public void add(int index, Object JavaDoc object)
146         throws TooManyTasksException, QueueClosedException
147     {
148         if (closed)
149             throw new QueueClosedException("Queue closed.");
150         synchronized (list) {
151             if (list.size() >= limit) {
152                 throw new TooManyTasksException("Too many tasks in queue...");
153             }
154             list.add(index, object);
155             list.notify();
156         }
157     }
158     
159     /**
160      * Appends all of the elements in the specified collection to the end of
161      * this list, in the order that they are returned by the specified
162      * collection's iterator.
163      */

164     public void addAll(Collection JavaDoc c)
165         throws TooManyTasksException, QueueClosedException
166     {
167         if (closed)
168             throw new QueueClosedException("Queue closed.");
169         synchronized (list) {
170             if (list.size() >= limit) {
171                 throw new TooManyTasksException("Too many tasks in queue...");
172             }
173             list.addAll(c);
174             list.notify();
175         }
176     }
177     
178     
179     /**
180     *
181     */

182     public int size() {
183         synchronized (list) {
184             return list.size();
185         }
186     }
187     
188     // Start 4682740 - ORB to support standalone monitoring
189

190     /**
191      * Return the size of the queue, unsynchronized method.
192      */

193     public int getUnsyncSize () {
194         return list.size();
195     }
196     
197     /**
198      * Return the number of waiting Threads on the queue.
199      */

200     public int getUnsyncWaitingThreads () {
201         return waiters;
202     }
203     
204     /**
205      * Return a String with information about this queue. Good for monitoring.
206      */

207     public String JavaDoc toString() {
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
209         sb.append("BlockingQueue [TW=").append(waiters);
210         sb.append(", CS=").append(list.size());
211         sb.append(", MS=").append(limit).append("]");
212         return sb.toString();
213     }
214     // End 4682740 - ORB to support standalone monitoring
215

216     /**
217      * Remove a task from the queue. If there are no objects then the thread blocks. The thread
218      * will be notified if any object is added to the queue.
219      * @return An object from the queue.
220      */

221     public Object JavaDoc remove(boolean canWait)
222         throws InterruptedException JavaDoc, QueueClosedException
223     {
224         while (true) {
225             if (aborted) {
226                 throw new QueueClosedException("Queue closed....");
227             }
228             synchronized (list) {
229                 if (list.size() > 0) {
230                     //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
231
//Bug 4677074 begin
232
//_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
233
//Bug 4677074 end
234
return list.removeFirst();
235                 }
236                 
237                 if (closed) {
238                     throw new QueueClosedException("Queue closed....");
239                 } else {
240                     if (! canWait) {
241                         return null;
242                     }
243                     //System.out.println(Thread.currentThread().getName() + ": waiting....");
244
//Bug 4677074 begin
245
//_logger.log(Level.FINE,Thread.currentThread().getName() + ": waiting....");
246
//Bug 4677074 end
247
waiters++; // added for 4682740
248
list.wait();
249                                 waiters--; // added for 4682740
250
}
251             }
252         }
253     }
254
255     
256     /**
257      * Remove a task from the queue. If there are no objects then the thread blocks. The thread
258      * will be notified if any object is added to the queue.
259      * @return An object from the queue.
260      */

261     public Object JavaDoc remove(long waitFor)
262         throws InterruptedException JavaDoc, QueueClosedException
263     {
264         // Fixed for Bug No. 4673949
265
if (aborted) {
266             throw new QueueClosedException("Queue closed....");
267         }
268         synchronized (list) {
269             if (list.size() > 0) {
270                 //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
271
//Bug 4677074 begin
272
//_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
273
//Bug 4677074 end
274
return list.removeFirst();
275             }
276
277             if (closed) {
278                 throw new QueueClosedException("Queue closed....");
279             } else {
280                 waiters++; // added for 4682740
281
list.wait(waitFor);
282                 waiters--; // added for 4682740
283
if (list.size() > 0) {
284                     //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
285
//Bug 4677074 begin
286
//_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
287
//Bug 4677074 end
288
return list.removeFirst();
289                 } else {
290                     // We timed out
291
return null;
292                 }
293             }
294         } //Synchronized list
295
}
296
297     public void shutdown() {
298         this.closed = true;
299         synchronized (list) {
300             list.notifyAll();
301         }
302     }
303     
304     public void abort() {
305         this.closed = this.aborted = true;
306         synchronized (list) {
307             list.notifyAll();
308         }
309     }
310     
311     
312 }
313
Popular Tags