KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResourceLimitingPool;
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.thread.ThreadControl;
29 import org.apache.excalibur.thread.ThreadPool;
30
31 /*
32 *
33  * A Thread Pool which can be configured to have a hard limit on the maximum number of threads
34  * which will be allocated. This is very important for servers to avoid running out of system
35  * resources. The pool can be configured to block for a new thread or throw an exception.
36  * The maximum block time can also be set.
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39  * @version CVS $Revision: 1.6 $ $Date: 2004/03/29 17:22:49 $
40  * @since 4.1
41  */

42 public class ResourceLimitingThreadPool
43     extends ThreadGroup JavaDoc
44     implements ObjectFactory, LogEnabled, Disposable, ThreadPool
45 {
46     private ResourceLimitingPool m_underlyingPool;
47
48     /**
49      * The associated thread pool.
50      */

51     private BasicThreadPool m_pool;
52
53     /*---------------------------------------------------------------
54      * Constructors
55      *-------------------------------------------------------------*/

56
57     /**
58      * Creates a new <code>ResourceLimitingThreadPool</code>.
59      *
60      * @param max Maximum number of Poolables which can be stored in the pool, 0 implies no limit.
61      */

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

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

98     public ResourceLimitingThreadPool( final String JavaDoc name,
99                                        final int max,
100                                        final boolean maxStrict,
101                                        final boolean blocking,
102                                        final long blockTimeout,
103                                        final long trimInterval )
104     {
105         super( name );
106
107         m_underlyingPool =
108             new ResourceLimitingPool( this, max, maxStrict,
109                                       blocking, blockTimeout,
110                                       trimInterval );
111         try
112         {
113             m_pool = new BasicThreadPool( this, name, m_underlyingPool );
114         }
115         catch( Exception JavaDoc e )
116         {
117             final String JavaDoc message = "Unable to create ThreadPool due to " + e;
118             throw new IllegalStateException JavaDoc( message );
119         }
120     }
121
122     /**
123      * Return the number of worker threads in the pool.
124      *
125      * @return the numebr of worker threads in the pool.
126      */

127     public int getSize()
128     {
129         return m_underlyingPool.getSize();
130     }
131
132     public void enableLogging( final Logger logger )
133     {
134         ContainerUtil.enableLogging( m_pool, logger );
135     }
136
137     public void dispose()
138     {
139         m_pool.dispose();
140     }
141
142     public Object JavaDoc newInstance()
143     {
144         return m_pool.newInstance();
145     }
146
147     public void decommission( final Object JavaDoc object )
148     {
149         m_pool.decommission( object );
150     }
151
152     public Class JavaDoc getCreatedClass()
153     {
154         return m_pool.getCreatedClass();
155     }
156
157     /**
158      * Run work in separate thread.
159      * Return a valid ThreadControl to control work thread.
160      *
161      * @param work the work to be executed.
162      * @return the ThreadControl
163      */

164     public ThreadControl execute( final Executable work )
165     {
166         return m_pool.execute( work );
167     }
168
169     /**
170      * Run work in separate thread.
171      * Return a valid ThreadControl to control work thread.
172      *
173      * @param work the work to be executed.
174      * @return the ThreadControl
175      */

176     public ThreadControl execute( final Runnable JavaDoc work )
177     {
178         return m_pool.execute( work );
179     }
180
181     /**
182      * Run work in separate thread.
183      * Return a valid ThreadControl to control work thread.
184      *
185      * @param work the work to be executed.
186      * @return the ThreadControl
187      */

188     public ThreadControl execute( final org.apache.excalibur.thread.Executable work )
189     {
190         return m_pool.execute( work );
191     }
192 }
193
Popular Tags