View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ThreadPool.java,v 1.2 2006/02/23 11:07:05 tanderson Exp $
44   */
45  package org.exolab.jms.common.threads;
46  
47  
48  import EDU.oswego.cs.dl.util.concurrent.Channel;
49  import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
50  import EDU.oswego.cs.dl.util.concurrent.SynchronousChannel;
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  import org.exolab.jms.common.threads.ThreadFactory;
54  import org.exolab.jms.common.threads.ThreadListener;
55  
56  
57  /***
58   * Thread pool.
59   *
60   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61   * @version $Revision: 1.2 $ $Date: 2006/02/23 11:07:05 $
62   */
63  public class ThreadPool extends PooledExecutor {
64  
65      /***
66       * The listener. May be <code>null</code>.
67       */
68      private ThreadListener _listener;
69  
70      /***
71       * The logger.
72       */
73      private static final Log _log = LogFactory.getLog(ThreadPool.class);
74  
75  
76      /***
77       * Construct a new <code>ThreadPool</code>.
78       *
79       * @param name        the name to assign the thread group and worker
80       *                    threads
81       * @param maxPoolSize the maximum no. of threads to use
82       */
83      public ThreadPool(String name, int maxPoolSize) {
84          this(name, maxPoolSize, false);
85      }
86  
87      /***
88       * Construct a new <code>ThreadPool</code>.
89       *
90       * @param name        the name to assign the thread group and worker
91       *                    threads
92       * @param channel     the channel for queueing
93       * @param maxPoolSize the maximum no. of threads to use
94       */
95      public ThreadPool(String name, Channel channel, int maxPoolSize) {
96          this(name, channel, maxPoolSize, false);
97      }
98  
99      /***
100      * Construct a new <code>ThreadPool</code>.
101      *
102      * @param name        the name of the pool
103      * @param maxPoolSize the maximum no. of threads to use
104      * @param daemon      if <code>true</code> all threads will be daemon
105      *                    threads
106      */
107     public ThreadPool(String name, int maxPoolSize, boolean daemon) {
108         this(new ThreadGroup(name), name, new SynchronousChannel(), maxPoolSize,
109                 daemon);
110     }
111 
112     /***
113      * Construct a new <code>ThreadPool</code>.
114      *
115      * @param name        the name of the pool
116      * @param channel     the channel for queueing
117      * @param maxPoolSize the maximum no. of threads to use
118      * @param daemon      if <code>true</code> all threads will be daemon
119      *                    threads
120      */
121     public ThreadPool(String name, Channel channel, int maxPoolSize,
122                       boolean daemon) {
123         this(new ThreadGroup(name), name, channel, maxPoolSize, daemon);
124     }
125 
126     /***
127      * Construct a new <code>ThreadPool</code>.
128      *
129      * @param group       the thread group. May be <code>null</code>
130      * @param name        the name to assign worker threads
131      * @param maxPoolSize the maximum no. of threads to use
132      */
133     public ThreadPool(ThreadGroup group, String name, int maxPoolSize) {
134         this(group, name, new SynchronousChannel(), maxPoolSize, false);
135     }
136 
137     /***
138      * Construct a new <code>ThreadPool</code>.
139      *
140      * @param group       the thread group. May be <code>null</code>
141      * @param name        the name to assign worker threads
142      * @param channel     the channel for queueing
143      * @param maxPoolSize the maximum no. of threads to use
144      * @param daemon      if <code>true</code> all threads will be daemon
145      *                    threads
146      */
147     public ThreadPool(ThreadGroup group, String name, Channel channel,
148                       int maxPoolSize, boolean daemon) {
149         super(channel, maxPoolSize);
150         setThreadFactory(new ThreadFactory(group, name + "-Worker-", daemon));
151     }
152 
153     /***
154      * Sets a listener to be notified when a thread processes a command.
155      *
156      * @param listener the listener
157      */
158     public void setThreadListener(ThreadListener listener) {
159         _listener = listener;
160     }
161 
162     /***
163      * Arrange for the given command to be executed by a thread in this pool.
164      * The method normally returns when the command has been handed off for
165      * (possibly later) execution.
166      */
167     public void execute(Runnable command) throws InterruptedException {
168         ThreadListener listener = _listener;
169         if (listener != null) {
170             super.execute(new NotifyingRunnable(command, listener));
171         } else {
172             super.execute(command);
173         }
174     }
175 
176     private static class NotifyingRunnable implements Runnable {
177 
178         /***
179          * The command to execute.
180          */
181         private final Runnable _command;
182 
183         /***
184          * The listener.
185          */
186         private final ThreadListener _listener;
187 
188 
189         /***
190          * Construct a new <code>NotifyingRunnable</code>.
191          *
192          * @param command  the command to execute
193          * @param listener the listener
194          */
195         public NotifyingRunnable(Runnable command, ThreadListener listener) {
196             _command = command;
197             _listener = listener;
198         }
199 
200         /***
201          * Run the command, notifying the listener.
202          *
203          * @see Thread#run()
204          */
205         public void run() {
206             try {
207                 _listener.begin(_command);
208             } catch (Throwable exception) {
209                 _log.error(exception, exception);
210             }
211             _command.run();
212             try {
213                 _listener.end(_command);
214             } catch (Throwable exception) {
215                 _log.error(exception, exception);
216             }
217         }
218     }
219 
220 }