KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > BlockingQueue


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.util.*;
54 import com.protomatter.pool.*;
55 import com.protomatter.syslog.Syslog;
56
57 /**
58  * A thread-blocking queue. The <tt>getNextObject()</tt> method blocks until
59  * there's something added to the queue.
60  */

61 public final class BlockingQueue
62 extends GrowingObjectPool
63 {
64   /**
65    * Create a new queue.
66    */

67   public BlockingQueue()
68   {
69     super(false);
70     try
71     {
72       init(new Hashtable());
73     }
74     catch (Exception JavaDoc x)
75     {
76       // never get here.
77
Syslog.log(this, x);
78     }
79   }
80
81   public ObjectPoolObject createObjectPoolObject()
82   {
83     return null;
84   }
85
86   /**
87    * Get the length of the queue (number of work elements yet to
88    * be processed).
89    */

90   public int getQueueLength()
91   {
92     return getObjectPoolSize();
93   }
94
95   /**
96    * Get the next object in the queue. This method blocks
97    * until something has been added.
98    */

99   public final Object JavaDoc getNextObject()
100   {
101     BlockingQueueOPO o = null;
102     while (true)
103     {
104       try
105       {
106         o = (BlockingQueueOPO)checkout();
107       }
108       catch (Exception JavaDoc x)
109       {
110         ; // nothin to do here but try again.
111
}
112       if (o != null)
113         return o.getObject();
114       Thread.yield();
115     }
116   }
117
118   /**
119    * Add an object to the queue. If there is a thread blocked waiting
120    * on the <tt>getNextObject()</tt> method, it will be un-blocked.
121    */

122   public final void add(Object JavaDoc object)
123   {
124     try
125     {
126       checkin(new BlockingQueueOPO(object));
127     }
128     catch (Exception JavaDoc x)
129     {
130       // never get here.
131
Syslog.log(this, x);
132     }
133   }
134
135   private class BlockingQueueOPO
136   implements ObjectPoolObject
137   {
138     private Object JavaDoc object = null;
139     private boolean valid = true;
140
141     public BlockingQueueOPO(Object JavaDoc o)
142     {
143       this.object = o;
144       this.valid = true;
145     }
146
147     public Object JavaDoc getObject()
148     {
149       return this.object;
150     }
151
152     public void deleteObjectPoolObject()
153     {
154       this.valid = false;
155     }
156
157     public boolean isObjectPoolObjectValid()
158     {
159       return this.valid;
160     }
161
162     public void beforeObjectPoolObjectCheckout()
163     {
164     }
165
166     public void afterObjectPoolObjectCheckin()
167     {
168     }
169   }
170 }
171
Popular Tags