KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > thread > DefaultThreadPool


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

17
18 package org.apache.james.util.thread;
19
20 //package org.apache.avalon.excalibur.thread.impl;
21

22 import org.apache.avalon.excalibur.pool.ObjectFactory;
23 import org.apache.avalon.excalibur.pool.HardResourceLimitingPool;
24 import org.apache.avalon.excalibur.thread.ThreadControl;
25 import org.apache.avalon.excalibur.thread.ThreadPool;
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.activity.Executable;
28 import org.apache.avalon.framework.logger.LogEnabled;
29 import org.apache.avalon.framework.logger.LogKitLogger;
30 import org.apache.avalon.framework.logger.Loggable;
31 import org.apache.avalon.framework.logger.Logger;
32 import org.apache.excalibur.threadcontext.ThreadContext;
33
34 /**
35  * This class is the public frontend for the thread pool code.
36  *
37  */

38 public class DefaultThreadPool
39    extends ThreadGroup JavaDoc
40    implements ObjectFactory, Loggable, LogEnabled, Disposable, ThreadPool
41 {
42        private HardResourceLimitingPool m_pool;
43
44        private int m_level;
45
46        private Logger m_logger;
47
48        private ThreadContext m_context;
49
50        public DefaultThreadPool( final int capacity )
51                throws Exception JavaDoc
52        {
53            this( "Worker Pool", capacity );
54        }
55
56        public DefaultThreadPool( final String JavaDoc name, final int capacity )
57                throws Exception JavaDoc
58        {
59            this( name, capacity, null );
60        }
61
62        public DefaultThreadPool( final String JavaDoc name,
63                                  final int min, final int max,
64                                  final ThreadContext context )
65                throws Exception JavaDoc
66        {
67            super( name );
68            m_pool = new HardResourceLimitingPool( this, min, max );
69            /* AbstractPool will initialize non-Initializable pools, so
70             * we have to initialize otherwise ... sheer idiocy */

71            if(m_pool instanceof org.apache.avalon.framework.activity.Initializable)
72            {
73                m_pool.initialize();
74            }
75            m_context = context;
76        }
77
78        public DefaultThreadPool( final String JavaDoc name,
79                                  final int capacity,
80                                  final ThreadContext context )
81                throws Exception JavaDoc
82        {
83            super( name );
84            m_pool = new HardResourceLimitingPool( this, capacity );
85            /* AbstractPool will initialize non-Initializable pools, so
86             * we have to initialize otherwise ... sheer idiocy */

87            if(m_pool instanceof org.apache.avalon.framework.activity.Initializable)
88            {
89                m_pool.initialize();
90            }
91            m_context = context;
92        }
93
94        public void setLogger( final org.apache.log.Logger logger )
95        {
96            enableLogging( new LogKitLogger( logger ) );
97        }
98
99        public void enableLogging( final Logger logger )
100        {
101            m_logger = logger;
102            m_pool.enableLogging( m_logger );
103        }
104
105        public void dispose()
106        {
107            m_pool.dispose();
108            m_pool = null;
109        }
110
111        public Object JavaDoc newInstance()
112        {
113            final String JavaDoc name = getName() + " Worker #" + m_level++;
114
115            ThreadContext context = null;
116            if( null != m_context )
117            {
118                context = m_context.duplicate();
119            }
120
121            final WorkerThread worker =
122                                       new WorkerThread( this, name, m_pool, context );
123            worker.setDaemon( true );
124            worker.enableLogging( m_logger );
125            worker.start();
126            return worker;
127        }
128
129        public void decommission( final Object JavaDoc object )
130        {
131            if( object instanceof WorkerThread )
132            {
133                ((WorkerThread)object).dispose();
134            }
135        }
136
137        public Class JavaDoc getCreatedClass()
138        {
139            return WorkerThread.class;
140        }
141
142     /**
143      * Run work in separate thread.
144      * Return a valid ThreadControl to control work thread.
145      *
146      * @param work the work to be executed.
147      * @return the ThreadControl
148      */

149        public ThreadControl execute( final Runnable JavaDoc work )
150        {
151            return execute( new ExecutableRunnable( work ) );
152        }
153
154     /**
155      * Run work in separate thread.
156      * Return a valid ThreadControl to control work thread.
157      *
158      * @param work the work to be executed.
159      * @return the ThreadControl
160      */

161        public ThreadControl execute( final Executable work )
162        {
163            final WorkerThread worker = getWorker();
164            return worker.execute( work );
165        }
166
167     /**
168      * Retrieve a worker thread from pool.
169      *
170      * @return the worker thread retrieved from pool
171      */

172        protected WorkerThread getWorker()
173        {
174            try
175            {
176                return (WorkerThread)m_pool.get();
177            }
178            catch( final Exception JavaDoc e )
179            {
180                throw new IllegalStateException JavaDoc( "Unable to access thread pool due to " + e );
181            }
182        }
183 }
184
Popular Tags