KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > thread > impl > InstrumentedResourceLimitingThreadPool


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.thread.impl;
18
19 import org.apache.avalon.excalibur.pool.ObjectFactory;
20 import org.apache.avalon.excalibur.pool.InstrumentedResourceLimitingPool;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.activity.Executable;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.avalon.framework.logger.LogEnabled;
26 import org.apache.avalon.framework.logger.Logger;
27
28 import org.apache.excalibur.instrument.Instrument;
29 import org.apache.excalibur.instrument.Instrumentable;
30
31 import org.apache.excalibur.thread.ThreadControl;
32 import org.apache.excalibur.thread.ThreadPool;
33
34 /**
35  * A Thread Pool which can be configured to have a hard limit on the maximum number of threads
36  * which will be allocated. This is very important for servers to avoid running out of system
37  * resources. The pool can be configured to block for a new thread or throw an exception.
38  * The maximum block time can also be set.
39  *
40  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
41  * @version CVS $Revision: 1.1 $ $Date: 2004/03/29 17:22:49 $
42  * @since 4.1
43  */

44 public class InstrumentedResourceLimitingThreadPool
45     extends ThreadGroup JavaDoc
46     implements ObjectFactory, LogEnabled, Disposable, ThreadPool, Instrumentable
47 {
48     private InstrumentedResourceLimitingPool m_underlyingPool;
49
50     /** Instrumentable Name assigned to this Instrumentable */
51     private String JavaDoc m_instrumentableName;
52
53     /**
54      * The associated thread pool.
55      */

56     private BasicThreadPool m_pool;
57
58     /*---------------------------------------------------------------
59      * Constructors
60      *-------------------------------------------------------------*/

61
62     /**
63      * Creates a new <code>ResourceLimitingThreadPool</code>.
64      *
65      * @param max Maximum number of Poolables which can be stored in the pool, 0 implies no limit.
66      */

67     public InstrumentedResourceLimitingThreadPool( final int max )
68     {
69         this( "Worker Pool", max );
70     }
71
72     /**
73      * Creates a new <code>ResourceLimitingThreadPool</code> with maxStrict enabled,
74      * blocking enabled, no block timeout and a trim interval of 10 seconds.
75      *
76      * @param name Name which will used as the thread group name as well as the prefix of the
77      * names of all threads created by the pool.
78      * @param max Maximum number of WorkerThreads which can be stored in the pool,
79      * 0 implies no limit.
80      */

81     public InstrumentedResourceLimitingThreadPool( final String JavaDoc name, final int max )
82     {
83         this( name, max, true, true, 0, 10000 );
84     }
85
86     /**
87      * Creates a new <code>ResourceLimitingThreadPool</code>.
88      *
89      * @param name Name which will used as the thread group name as well as the prefix of the
90      * names of all threads created by the pool.
91      * @param max Maximum number of WorkerThreads which can be stored in the pool,
92      * 0 implies no limit.
93      * @param maxStrict true if the pool should never allow more than max WorkerThreads to
94      * be created. Will cause an exception to be thrown if more than max WorkerThreads are
95      * requested and blocking is false.
96      * @param blocking true if the pool should cause a thread calling get() to block when
97      * WorkerThreads are not currently available on the pool.
98      * @param blockTimeout The maximum amount of time, in milliseconds, that a call to get() will
99      * block before an exception is thrown. A value of 0 implies an indefinate wait.
100      * @param trimInterval The minimum interval with which old unused WorkerThreads will be
101      * removed from the pool. A value of 0 will cause the pool to never trim WorkerThreads.
102      */

103     public InstrumentedResourceLimitingThreadPool( final String JavaDoc name,
104                                        final int max,
105                                        final boolean maxStrict,
106                                        final boolean blocking,
107                                        final long blockTimeout,
108                                        final long trimInterval )
109     {
110         super( name );
111
112         m_underlyingPool =
113             new InstrumentedResourceLimitingPool( this, max, maxStrict,
114                                       blocking, blockTimeout,
115                                       trimInterval );
116         try
117         {
118             m_pool = new BasicThreadPool( this, name, m_underlyingPool );
119         }
120         catch( Exception JavaDoc e )
121         {
122             final String JavaDoc message = "Unable to create ThreadPool due to " + e;
123             throw new IllegalStateException JavaDoc( message );
124         }
125     }
126
127     /*---------------------------------------------------------------
128      * Instrumentable Methods
129      *-------------------------------------------------------------*/

130     /**
131      * Sets the name for the Instrumentable. The Instrumentable Name is used
132      * to uniquely identify the Instrumentable during the configuration of
133      * the InstrumentManager and to gain access to an InstrumentableDescriptor
134      * through the InstrumentManager. The value should be a string which does
135      * not contain spaces or periods.
136      * <p>
137      * This value may be set by a parent Instrumentable, or by the
138      * InstrumentManager using the value of the 'instrumentable' attribute in
139      * the configuration of the component.
140      *
141      * @param name The name used to identify a Instrumentable.
142      */

143     public void setInstrumentableName( String JavaDoc name )
144     {
145         m_instrumentableName = name;
146     }
147
148     /**
149      * Gets the name of the Instrumentable.
150      *
151      * @return The name used to identify a Instrumentable.
152      */

153     public String JavaDoc getInstrumentableName()
154     {
155         return m_instrumentableName;
156     }
157
158     /**
159      * Obtain a reference to all the Instruments that the Instrumentable object
160      * wishes to expose. All sampling is done directly through the
161      * Instruments as opposed to the Instrumentable interface.
162      *
163      * @return An array of the Instruments available for profiling. Should
164      * never be null. If there are no Instruments, then
165      * EMPTY_INSTRUMENT_ARRAY can be returned. This should never be
166      * the case though unless there are child Instrumentables with
167      * Instruments.
168      */

169     public Instrument[] getInstruments()
170     {
171         return Instrumentable.EMPTY_INSTRUMENT_ARRAY;
172     }
173
174     /**
175      * Any Object which implements Instrumentable can also make use of other
176      * Instrumentable child objects. This method is used to tell the
177      * InstrumentManager about them.
178      *
179      * @return An array of child Instrumentables. This method should never
180      * return null. If there are no child Instrumentables, then
181      * EMPTY_INSTRUMENTABLE_ARRAY can be returned.
182      */

183     public Instrumentable[] getChildInstrumentables()
184     {
185         return new Instrumentable[]{m_underlyingPool};
186     }
187
188     /**
189      * Return the number of worker threads in the pool.
190      *
191      * @return the numebr of worker threads in the pool.
192      */

193     public int getSize()
194     {
195         return m_underlyingPool.getSize();
196     }
197
198     public void enableLogging( final Logger logger )
199     {
200         ContainerUtil.enableLogging( m_pool, logger );
201     }
202
203     public void dispose()
204     {
205         m_pool.dispose();
206     }
207
208     public Object JavaDoc newInstance()
209     {
210         return m_pool.newInstance();
211     }
212
213     public void decommission( final Object JavaDoc object )
214     {
215         m_pool.decommission( object );
216     }
217
218     public Class JavaDoc getCreatedClass()
219     {
220         return m_pool.getCreatedClass();
221     }
222
223     /**
224      * Run work in separate thread.
225      * Return a valid ThreadControl to control work thread.
226      *
227      * @param work the work to be executed.
228      * @return the ThreadControl
229      */

230     public ThreadControl execute( final Executable work )
231     {
232         return m_pool.execute( work );
233     }
234
235     /**
236      * Run work in separate thread.
237      * Return a valid ThreadControl to control work thread.
238      *
239      * @param work the work to be executed.
240      * @return the ThreadControl
241      */

242     public ThreadControl execute( final Runnable JavaDoc work )
243     {
244         return m_pool.execute( work );
245     }
246
247     /**
248      * Run work in separate thread.
249      * Return a valid ThreadControl to control work thread.
250      *
251      * @param work the work to be executed.
252      * @return the ThreadControl
253      */

254     public ThreadControl execute( final org.apache.excalibur.thread.Executable work )
255     {
256         return m_pool.execute( work );
257     }
258 }
259
Popular Tags