KickJava   Java API By Example, From Geeks To Geeks.

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


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.SoftResourceLimitingPool;
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  * This class is the public frontend for the thread pool code.
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  */

36 public class DefaultThreadPool
37     extends ThreadGroup JavaDoc
38     implements ObjectFactory, LogEnabled, Disposable, ThreadPool
39 {
40     private final BasicThreadPool m_pool;
41     private SoftResourceLimitingPool m_underlyingPool;
42
43     public DefaultThreadPool( final int capacity )
44         throws Exception JavaDoc
45     {
46         this( "Worker Pool", capacity );
47     }
48
49     public DefaultThreadPool( final String JavaDoc name,
50                               final int capacity )
51         throws Exception JavaDoc
52     {
53         super( name );
54         m_underlyingPool = new SoftResourceLimitingPool( this, capacity );
55         m_pool = new BasicThreadPool( this, name, m_underlyingPool );
56     }
57
58     public DefaultThreadPool( final String JavaDoc name,
59                               final int min,
60                               final int max )
61         throws Exception JavaDoc
62     {
63         super( name );
64         m_underlyingPool = new SoftResourceLimitingPool( this, min, max );
65         m_pool = new BasicThreadPool( this, name, m_underlyingPool );
66     }
67
68     public void enableLogging( final Logger logger )
69     {
70         ContainerUtil.enableLogging( m_pool, logger );
71     }
72
73     public void dispose()
74     {
75         m_pool.dispose();
76     }
77
78     public Object JavaDoc newInstance()
79     {
80         return m_pool.newInstance();
81     }
82
83     public void decommission( final Object JavaDoc object )
84     {
85         m_pool.decommission( object );
86     }
87
88     public Class JavaDoc getCreatedClass()
89     {
90         return m_pool.getCreatedClass();
91     }
92
93     /**
94      * Run work in separate thread.
95      * Return a valid ThreadControl to control work thread.
96      *
97      * @param work the work to be executed.
98      * @return the ThreadControl
99      */

100     public ThreadControl execute( final Executable work )
101     {
102         return m_pool.execute( work );
103     }
104
105     /**
106      * Run work in separate thread.
107      * Return a valid ThreadControl to control work thread.
108      *
109      * @param work the work to be executed.
110      * @return the ThreadControl
111      */

112     public ThreadControl execute( final Runnable JavaDoc work )
113     {
114         return m_pool.execute( work );
115     }
116
117     /**
118      * Run work in separate thread.
119      * Return a valid ThreadControl to control work thread.
120      *
121      * @param work the work to be executed.
122      * @return the ThreadControl
123      */

124     public ThreadControl execute( final org.apache.excalibur.thread.Executable work )
125     {
126         return m_pool.execute( work );
127     }
128 }
129
Popular Tags