View Javadoc
1   package org.apache.commons.jcs3.utils.threadpool;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * This object holds configuration data for a thread pool.
24   */
25  public final class PoolConfiguration
26      implements Cloneable
27  {
28      /**
29       * DEFAULT SETTINGS
30       */
31      private static final boolean DEFAULT_USE_BOUNDARY = true;
32  
33      /** Default queue size limit */
34      private static final int DEFAULT_BOUNDARY_SIZE = 2000;
35  
36      /** Default max size */
37      private static final int DEFAULT_MAXIMUM_POOL_SIZE = 150;
38  
39      /** Default min */
40      private static final int DEFAULT_MINIMUM_POOL_SIZE = Runtime.getRuntime().availableProcessors();
41  
42      /** Default keep alive */
43      private static final int DEFAULT_KEEPALIVE_TIME = 1000 * 60 * 5;
44  
45      /** Default when blocked */
46      private static final WhenBlockedPolicy DEFAULT_WHEN_BLOCKED_POLICY = WhenBlockedPolicy.RUN;
47  
48      /** Default startup size */
49      private static final int DEFAULT_STARTUP_SIZE = DEFAULT_MINIMUM_POOL_SIZE;
50  
51      /** Should we bound the queue */
52      private boolean useBoundary = DEFAULT_USE_BOUNDARY;
53  
54      /** If the queue is bounded, how big can it get */
55      private int boundarySize = DEFAULT_BOUNDARY_SIZE;
56  
57      /** only has meaning if a boundary is used */
58      private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
59  
60      /**
61       * the exact number that will be used in a boundless queue. If the queue has a boundary, more
62       * will be created if the queue fills.
63       */
64      private int minimumPoolSize = DEFAULT_MINIMUM_POOL_SIZE;
65  
66      /** How long idle threads above the minimum should be kept alive. */
67      private int keepAliveTime = DEFAULT_KEEPALIVE_TIME;
68  
69      public enum WhenBlockedPolicy {
70          /** abort when queue is full and max threads is reached. */
71          ABORT,
72  
73          /** block when queue is full and max threads is reached. */
74          BLOCK,
75  
76          /** run in current thread when queue is full and max threads is reached. */
77          RUN,
78  
79          /** wait when queue is full and max threads is reached. */
80          WAIT,
81  
82          /** discard oldest when queue is full and max threads is reached. */
83          DISCARDOLDEST
84      }
85  
86      /** should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */
87      private WhenBlockedPolicy whenBlockedPolicy = DEFAULT_WHEN_BLOCKED_POLICY;
88  
89      /** The number of threads to create on startup */
90      private int startUpSize = DEFAULT_MINIMUM_POOL_SIZE;
91  
92      /**
93       * @param useBoundary The useBoundary to set.
94       */
95      public void setUseBoundary( final boolean useBoundary )
96      {
97          this.useBoundary = useBoundary;
98      }
99  
100     /**
101      * @return Returns the useBoundary.
102      */
103     public boolean isUseBoundary()
104     {
105         return useBoundary;
106     }
107 
108     /**
109      * Default
110      */
111     public PoolConfiguration()
112     {
113         this( DEFAULT_USE_BOUNDARY, DEFAULT_BOUNDARY_SIZE, DEFAULT_MAXIMUM_POOL_SIZE,
114               DEFAULT_MINIMUM_POOL_SIZE, DEFAULT_KEEPALIVE_TIME,
115               DEFAULT_WHEN_BLOCKED_POLICY, DEFAULT_STARTUP_SIZE );
116     }
117 
118     /**
119      * Construct a completely configured instance.
120      * <p>
121      * @param useBoundary
122      * @param boundarySize
123      * @param maximumPoolSize
124      * @param minimumPoolSize
125      * @param keepAliveTime
126      * @param whenBlockedPolicy
127      * @param startUpSize
128      */
129     public PoolConfiguration( final boolean useBoundary, final int boundarySize, final int maximumPoolSize, final int minimumPoolSize,
130                               final int keepAliveTime, final WhenBlockedPolicy whenBlockedPolicy, final int startUpSize )
131     {
132         setUseBoundary( useBoundary );
133         setBoundarySize( boundarySize );
134         setMaximumPoolSize( maximumPoolSize );
135         setMinimumPoolSize( minimumPoolSize );
136         setKeepAliveTime( keepAliveTime );
137         setWhenBlockedPolicy( whenBlockedPolicy );
138         setStartUpSize( startUpSize );
139     }
140 
141     /**
142      * @param boundarySize The boundarySize to set.
143      */
144     public void setBoundarySize( final int boundarySize )
145     {
146         this.boundarySize = boundarySize;
147     }
148 
149     /**
150      * @return Returns the boundarySize.
151      */
152     public int getBoundarySize()
153     {
154         return boundarySize;
155     }
156 
157     /**
158      * @param maximumPoolSize The maximumPoolSize to set.
159      */
160     public void setMaximumPoolSize( final int maximumPoolSize )
161     {
162         this.maximumPoolSize = maximumPoolSize;
163     }
164 
165     /**
166      * @return Returns the maximumPoolSize.
167      */
168     public int getMaximumPoolSize()
169     {
170         return maximumPoolSize;
171     }
172 
173     /**
174      * @param minimumPoolSize The minimumPoolSize to set.
175      */
176     public void setMinimumPoolSize( final int minimumPoolSize )
177     {
178         this.minimumPoolSize = minimumPoolSize;
179     }
180 
181     /**
182      * @return Returns the minimumPoolSize.
183      */
184     public int getMinimumPoolSize()
185     {
186         return minimumPoolSize;
187     }
188 
189     /**
190      * @param keepAliveTime The keepAliveTime to set.
191      */
192     public void setKeepAliveTime( final int keepAliveTime )
193     {
194         this.keepAliveTime = keepAliveTime;
195     }
196 
197     /**
198      * @return Returns the keepAliveTime.
199      */
200     public int getKeepAliveTime()
201     {
202         return keepAliveTime;
203     }
204 
205     /**
206      * @param whenBlockedPolicy The whenBlockedPolicy to set.
207      */
208     public void setWhenBlockedPolicy( final String whenBlockedPolicy )
209     {
210         if ( whenBlockedPolicy != null )
211         {
212             final WhenBlockedPolicy policy = WhenBlockedPolicy.valueOf(whenBlockedPolicy.trim().toUpperCase());
213             setWhenBlockedPolicy(policy);
214         }
215         else
216         {
217             // the value is null, default to RUN
218             this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
219         }
220     }
221 
222     /**
223      * @param whenBlockedPolicy The whenBlockedPolicy to set.
224      */
225     public void setWhenBlockedPolicy( final WhenBlockedPolicy whenBlockedPolicy )
226     {
227         if ( whenBlockedPolicy != null )
228         {
229             this.whenBlockedPolicy = whenBlockedPolicy;
230         }
231         else
232         {
233             // the value is null, default to RUN
234             this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
235         }
236     }
237 
238     /**
239      * @return Returns the whenBlockedPolicy.
240      */
241     public WhenBlockedPolicy getWhenBlockedPolicy()
242     {
243         return whenBlockedPolicy;
244     }
245 
246     /**
247      * @param startUpSize The startUpSize to set.
248      */
249     public void setStartUpSize( final int startUpSize )
250     {
251         this.startUpSize = startUpSize;
252     }
253 
254     /**
255      * @return Returns the startUpSize.
256      */
257     public int getStartUpSize()
258     {
259         return startUpSize;
260     }
261 
262     /**
263      * To string for debugging purposes.
264      * @return String
265      */
266     @Override
267     public String toString()
268     {
269         final StringBuilder buf = new StringBuilder();
270         buf.append( "useBoundary = [" + isUseBoundary() + "] " );
271         buf.append( "boundarySize = [" + boundarySize + "] " );
272         buf.append( "maximumPoolSize = [" + maximumPoolSize + "] " );
273         buf.append( "minimumPoolSize = [" + minimumPoolSize + "] " );
274         buf.append( "keepAliveTime = [" + keepAliveTime + "] " );
275         buf.append( "whenBlockedPolicy = [" + getWhenBlockedPolicy() + "] " );
276         buf.append( "startUpSize = [" + startUpSize + "]" );
277         return buf.toString();
278     }
279 
280     /**
281      * Copies the instance variables to another instance.
282      * <p>
283      * @return PoolConfiguration
284      */
285     @Override
286     public PoolConfiguration clone()
287     {
288         return new PoolConfiguration( isUseBoundary(), boundarySize, maximumPoolSize, minimumPoolSize, keepAliveTime,
289                                       getWhenBlockedPolicy(), startUpSize );
290     }
291 }