KickJava   Java API By Example, From Geeks To Geeks.

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


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.Pool;
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.ThreadPool;
29 import org.apache.excalibur.thread.ThreadControl;
30 import org.apache.excalibur.thread.impl.AbstractThreadPool;
31 import org.apache.excalibur.thread.impl.WorkerThread;
32
33 /**
34  * The ThreadPool that binds to Legacy Pooling implementation.
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  */

38 class BasicThreadPool
39     extends AbstractThreadPool
40     implements ObjectFactory, LogEnabled, Disposable, ThreadPool
41 {
42     /**
43      * The underlying pool.
44      */

45     private Pool m_pool;
46
47     /**
48      * The logger to use for debugging purposes.
49      */

50     private Logger m_logger;
51
52     /**
53      * Create a new ThreadPool with specified capacity.
54      *
55      * @param threadGroup the thread group used in pool
56      * @param name the name of pool (used in naming threads)
57      * @param pool the underling pool
58      * @throws Exception if unable to create pool
59      */

60     public BasicThreadPool( final ThreadGroup JavaDoc threadGroup,
61                             final String JavaDoc name,
62                             final Pool pool )
63         throws Exception JavaDoc
64     {
65         super( name, threadGroup );
66         if( null == pool )
67         {
68             throw new NullPointerException JavaDoc( "pool" );
69         }
70
71         m_pool = pool;
72     }
73
74     /**
75      * Setup Logging.
76      *
77      * @param logger the logger
78      */

79     public void enableLogging( final Logger logger )
80     {
81         m_logger = logger;
82         ContainerUtil.enableLogging( m_pool, logger );
83     }
84
85     /**
86      * Dispose of underlying pool and cleanup resources.
87      */

88     public void dispose()
89     {
90         ContainerUtil.dispose( m_pool );
91         m_pool = null;
92     }
93
94     /**
95      * Create new Poolable instance.
96      *
97      * @return the new Poolable instance
98      */

99     public Object JavaDoc newInstance()
100     {
101         return createWorker();
102     }
103
104     /**
105      * Overide newWorkerThread to provide a WorkerThread
106      * that is Poolable and LogEnabled.
107      *
108      * @param name the name of WorkerThread
109      * @return the created WorkerThread
110      */

111     protected WorkerThread newWorkerThread( final String JavaDoc name )
112     {
113         final SimpleWorkerThread thread =
114             new SimpleWorkerThread( this, getThreadGroup(), name );
115         ContainerUtil.enableLogging( thread, m_logger.getChildLogger( "worker" ) );
116         return thread;
117     }
118
119     public void decommission( final Object JavaDoc object )
120     {
121         if( object instanceof WorkerThread )
122         {
123             destroyWorker( (WorkerThread)object );
124         }
125     }
126
127     /**
128      * Return the class of poolable instance.
129      *
130      * @return the class of poolable instance.
131      */

132     public Class JavaDoc getCreatedClass()
133     {
134         return SimpleWorkerThread.class;
135     }
136
137     /**
138      * Run work in separate thread.
139      * Return a valid ThreadControl to control work thread.
140      *
141      * @param work the work to be executed.
142      * @return the ThreadControl
143      */

144     public ThreadControl execute( final Executable work )
145     {
146          return execute( new ExecutableExecuteable( work ) );
147     }
148
149     /**
150      * Retrieve a worker thread from pool.
151      *
152      * @return the worker thread retrieved from pool
153      */

154     protected WorkerThread getWorker()
155     {
156         try
157         {
158             return (WorkerThread)m_pool.get();
159         }
160         catch( final Exception JavaDoc e )
161         {
162             final String JavaDoc message =
163                 "Unable to access thread pool due to " + e;
164             throw new IllegalStateException JavaDoc( message );
165         }
166     }
167
168     /**
169      * Release worker back into pool.
170      *
171      * FIX ME: do we want to verify if it is interrupted or interrupt the worker
172      * thread?
173      *
174      * @param worker the worker (Should be a {@link SimpleWorkerThread}).
175      */

176     protected void releaseWorker( final WorkerThread worker )
177     {
178         worker.clearInterruptFlag();
179         
180         // If the pool is disposed before the last worker has been released
181
// m_pool will be null. This can be difficult to avoid as there
182
// is no way to query whether or not all workers have actually been
183
// released. Underlying pool implementations should probably block
184
// on their dispose methods until all outstanding objects have been
185
// returned.
186
Pool pool = m_pool; // Be thread safe
187
if ( pool != null )
188         {
189             pool.put( (SimpleWorkerThread)worker );
190         }
191     }
192 }
193
Popular Tags