KickJava   Java API By Example, From Geeks To Geeks.

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


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.syslog.Syslog;
55 import com.protomatter.pool.*;
56
57 /**
58  * A work queue. Items of work (runnables) are added, and a pool
59  * of threads works them off in the background.
60  */

61 public final class WorkQueue
62 extends GrowingObjectPool
63 {
64   // just for naming the threads and thread group
65
private static int queueNumber = 0;
66
67   private int numThreads = 0;
68   private int id = 0;
69   private ThreadGroup JavaDoc threadGroup = null;
70
71   private static synchronized int nextQueueNumber()
72   {
73     return ++queueNumber;
74   }
75
76   /**
77    * Create a new workqueue with the given number of worker
78    * threads.
79    */

80   public WorkQueue(int numThreads)
81   {
82     this(null, numThreads);
83   }
84
85   /**
86    * Create a new workqueue with the given number of worker
87    * threads.
88    */

89   public WorkQueue(String JavaDoc name, int numThreads)
90   {
91     super(false);
92     try
93     {
94       init(new Hashtable());
95     }
96     catch (Exception JavaDoc x)
97     {
98       // never get here.
99
Syslog.log(this, x);
100     }
101
102     this.id = nextQueueNumber();
103     if (name == null)
104       this.threadGroup = new ThreadGroup JavaDoc("WorkQueueGroup[id="+id+"]");
105     else
106       this.threadGroup = new ThreadGroup JavaDoc("WorkQueueGroup[id="+id+", name="+name+"]");
107     this.threadGroup.setDaemon(true);
108
109     this.numThreads = numThreads;
110     for (int i=0; i<numThreads; i++)
111     {
112       WorkQueueThread t = new WorkQueueThread(this, threadGroup, id, name, i);
113       t.start();
114     }
115   }
116
117   public ObjectPoolObject createObjectPoolObject()
118   {
119     return null;
120   }
121
122   /**
123    * Get the number of worker threads associated with this
124    * WorkQueue.
125    */

126   public int getNumThreads()
127   {
128     return this.numThreads;
129   }
130
131   /**
132    * Get the number of threads that are currently performing work.
133    */

134   public int getNumWorkers()
135   {
136     return (this.numThreads - getNumWaiters());
137   }
138
139   /**
140    * Get the length of the queue (number of work elements yet to
141    * be processed).
142    */

143   public int getQueueLength()
144   {
145     return getObjectPoolSize();
146   }
147
148   /**
149    * Get the next piece of work to do. This method blocks
150    * until something has been added.
151    */

152   final Runnable JavaDoc getWork()
153   throws Exception JavaDoc
154   {
155     Object JavaDoc o = checkout();
156     if (o instanceof Runnable JavaDoc)
157       return (Runnable JavaDoc)o;
158     if (o != null)
159       return ((WorkQueueOPO)o).getRunnable();
160     return null;
161   }
162
163   /**
164    * Add a runnable to the list of things to do.
165    */

166   public final void addWork(Runnable JavaDoc r)
167   {
168     try
169     {
170       if (r instanceof ObjectPoolObject)
171         checkin((ObjectPoolObject)r);
172       else
173         checkin(new WorkQueueOPO(r));
174     }
175     catch (Exception JavaDoc x)
176     {
177       // never get here...
178
Syslog.log(this, x);
179     }
180   }
181
182   private class WorkQueueOPO
183   implements ObjectPoolObject
184   {
185     private Runnable JavaDoc runnable = null;
186     private boolean valid = true;
187
188     public WorkQueueOPO(Runnable JavaDoc r)
189     {
190       this.runnable = r;
191       this.valid = true;
192     }
193
194     public Runnable JavaDoc getRunnable()
195     {
196       return this.runnable;
197     }
198
199     public void deleteObjectPoolObject()
200     {
201       this.valid = false;
202     }
203
204     public boolean isObjectPoolObjectValid()
205     {
206       return this.valid;
207     }
208
209     public void beforeObjectPoolObjectCheckout()
210     {
211     }
212
213     public void afterObjectPoolObjectCheckin()
214     {
215     }
216   }
217 }
218
Popular Tags