View Javadoc

1   /**********************************************************************
2    * Jdk15ThreadPool.java
3    * created on 10.08.2006 by netseeker
4    * $Id: Jdk15ThreadPool.java,v 1.7 2006/11/10 00:35:14 netseeker Exp $
5    * $Log: Jdk15ThreadPool.java,v $
6    * Revision 1.7  2006/11/10 00:35:14  netseeker
7    * switched to maven2
8    *
9    * Revision 1.6  2006/11/05 16:34:39  netseeker
10   * Code cleanup, added support of JDK 1.5  thread pools
11   *
12   * Revision 1.5  2006/10/16 19:50:10  netseeker
13   * *** empty log message ***
14   *
15   * Revision 1.4  2006/10/11 22:40:31  netseeker
16   * *** empty log message ***
17   *
18   * Revision 1.3  2006/08/10 19:13:20  netseeker
19   * modified sources  to be compatible to compiler java source level 1.4
20   *
21   * Revision 1.2  2006/08/10 18:57:06  netseeker
22   * *** empty log message ***
23   *
24   * Revision 1.1  2006/08/10 18:36:04  netseeker
25   * added abstraction of ThreadPools and an Executor based implementation for Java >= 1.5
26   *
27   *
28   * ====================================================================
29   *
30   *  Copyright 2005-2006 netseeker aka Michael Manske
31   *
32   *  Licensed under the Apache License, Version 2.0 (the "License");
33   *  you may not use this file except in compliance with the License.
34   *  You may obtain a copy of the License at
35   *
36   *      http://www.apache.org/licenses/LICENSE-2.0
37   *
38   *  Unless required by applicable law or agreed to in writing, software
39   *  distributed under the License is distributed on an "AS IS" BASIS,
40   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41   *  See the License for the specific language governing permissions and
42   *  limitations under the License.
43   * ====================================================================
44   *
45   * This file is part of the EJOE framework.
46   * For more information on the author, please see
47   * <http://www.manskes.de/>.
48   *
49   *********************************************************************/
50  package de.netseeker.ejoe.concurrent;
51  
52  import java.util.concurrent.LinkedBlockingQueue;
53  import java.util.concurrent.ThreadPoolExecutor;
54  import java.util.concurrent.TimeUnit;
55  
56  /***
57   * Fixed size thread pool implementation for usage with java >= 1.5.0
58   * 
59   * @author netseeker
60   * @since 0.3.9.1
61   */
62  public class Jdk15ThreadPool implements ThreadService
63  {
64      private ThreadPoolExecutor _execService;
65  
66      /***
67       * 
68       */
69      public Jdk15ThreadPool()
70      {
71          this( 1 );
72      }
73  
74      /***
75       * @param numberOfThreads
76       */
77      public Jdk15ThreadPool(int numberOfThreads)
78      {
79          _execService = new ThreadPoolExecutor( numberOfThreads, numberOfThreads, 0L, TimeUnit.MILLISECONDS,
80                                                 new LinkedBlockingQueue(), new EJThreadFactory() );
81      }
82  
83      /***
84       * @param threadGroup
85       */
86      public Jdk15ThreadPool(final ThreadGroup threadGroup)
87      {
88          this( 1 );
89      }
90  
91      /***
92       * @param threadGroup
93       * @param numberOfThreads
94       */
95      public Jdk15ThreadPool(final ThreadGroup threadGroup, int numberOfThreads)
96      {
97          _execService = new ThreadPoolExecutor( numberOfThreads, numberOfThreads, 0L, TimeUnit.MILLISECONDS,
98                                                 new LinkedBlockingQueue(), new EJThreadFactory( threadGroup ) );
99          // _execService.prestartAllCoreThreads();
100     }
101 
102     /*
103      * (non-Javadoc)
104      * 
105      * @see de.netseeker.ejoe.concurrent.ThreadService#getActiveWorkerCount()
106      */
107     public int getActiveWorkerCount()
108     {
109         return _execService.getActiveCount();
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see de.netseeker.ejoe.concurrent.ThreadService#getCurrentPoolsize()
116      */
117     public int getCurrentPoolsize()
118     {
119         return _execService.getPoolSize();
120     }
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see de.netseeker.ejoe.concurrent.ThreadService#getExpectedPoolsize()
126      */
127     public int getExpectedPoolsize()
128     {
129         return _execService.getCorePoolSize();
130     }
131 
132     /*
133      * (non-Javadoc)
134      * 
135      * @see de.netseeker.ejoe.concurrent.ThreadService#invokeLater(java.lang.Runnable)
136      */
137     public void invokeLater( Runnable task )
138     {
139         _execService.execute( task );
140     }
141 
142     /*
143      * (non-Javadoc)
144      * 
145      * @see de.netseeker.ejoe.concurrent.ThreadService#resize(int)
146      */
147     public void resize( int poolSize )
148     {
149         _execService.setMaximumPoolSize( poolSize );
150 
151     }
152 
153     /*
154      * (non-Javadoc)
155      * 
156      * @see de.netseeker.ejoe.concurrent.ThreadService#stop()
157      */
158     public void stop()
159     {
160         _execService.shutdown();
161     }
162 }