KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > thread > impl > AbstractThreadPool


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.excalibur.thread.impl;
18
19 import org.apache.excalibur.thread.Executable;
20 import org.apache.excalibur.thread.ThreadControl;
21 import org.apache.excalibur.thread.ThreadPool;
22
23 /**
24  * This is the base class of all ThreadPools.
25  * Sub-classes should implement the abstract methods to
26  * retrieve and return Threads to the pool.
27  *
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  */

30 public abstract class AbstractThreadPool
31     implements ThreadPool
32 {
33     /**
34      * The thread group associated with pool.
35      */

36     private final ThreadGroup JavaDoc m_threadGroup;
37
38     /**
39      * The name of the thread pool.
40      * Used in naming threads.
41      */

42     private final String JavaDoc m_name;
43
44     /**
45      * A Running number that indicates the number
46      * of threads created by pool. Starts at 0 and
47      * increases.
48      */

49     private int m_level;
50
51     /**
52      * Create a ThreadPool with the specified name.
53      *
54      * @param name the name of thread pool (appears in thread group
55      * and thread names)
56      * @throws Exception if unable to create pool
57      */

58     public AbstractThreadPool( final String JavaDoc name,
59                                final ThreadGroup JavaDoc threadGroup )
60         throws Exception JavaDoc
61     {
62         if( null == name )
63         {
64             throw new NullPointerException JavaDoc( "name" );
65         }
66         if( null == threadGroup )
67         {
68             throw new NullPointerException JavaDoc( "threadGroup" );
69         }
70
71         m_name = name;
72         m_threadGroup = threadGroup;
73     }
74
75     /**
76      * Destroy a worker thread by scheduling it for shutdown.
77      *
78      * @param thread the worker thread
79      */

80     protected void destroyWorker( final WorkerThread thread )
81     {
82         thread.dispose();
83     }
84
85     /**
86      * Create a WorkerThread and start it up.
87      *
88      * @return the worker thread.
89      */

90     protected WorkerThread createWorker()
91     {
92         final String JavaDoc name = m_name + " Worker #" + m_level++;
93
94         final WorkerThread worker = newWorkerThread( name );
95         worker.setDaemon( true );
96         worker.start();
97         return worker;
98     }
99
100     /**
101      * Create a new worker for pool.
102      *
103      * @param name the name of worker
104      * @return the new WorkerThread
105      */

106     protected WorkerThread newWorkerThread( final String JavaDoc name )
107     {
108         return new WorkerThread( this, m_threadGroup, name );
109     }
110
111     /**
112      * Run work in separate thread.
113      * Return a valid ThreadControl to control work thread.
114      *
115      * @param work the work to be executed.
116      * @return the ThreadControl
117      */

118     public ThreadControl execute( final Runnable JavaDoc work )
119     {
120         return execute( new ExecutableRunnable( work ) );
121     }
122
123     /**
124      * Execute some executable work in a thread.
125      *
126      * @param work the work
127      * @return the ThreadControl
128      */

129     public ThreadControl execute( final Executable work )
130     {
131         final WorkerThread worker = getWorker();
132         return worker.execute( work );
133     }
134
135     /**
136      * Get the name used for thread pool.
137      * (Used in naming threads).
138      *
139      * @return the thread pool name
140      */

141     protected String JavaDoc getName()
142     {
143         return m_name;
144     }
145
146     /**
147      * Return the thread group that thread pool is associated with.
148      *
149      * @return the thread group that thread pool is associated with.
150      */

151     protected ThreadGroup JavaDoc getThreadGroup()
152     {
153         return m_threadGroup;
154     }
155
156     /**
157      * Retrieve a worker thread from pool.
158      *
159      * @return the worker thread retrieved from pool
160      */

161     protected abstract WorkerThread getWorker();
162
163     /**
164      * Return the WorkerThread to the pool.
165      *
166      * @param worker the worker thread to put back in pool
167      */

168     protected abstract void releaseWorker( final WorkerThread worker );
169 }
170
Popular Tags