1 package org.apache.commons.jcs.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 * <p>
25 * @author Aaron Smuts
26 */
27 public final class PoolConfiguration
28 implements Cloneable
29 {
30 /** Should we bound the queue */
31 private boolean useBoundary = true;
32
33 /** If the queue is bounded, how big can it get */
34 private int boundarySize = 2000;
35
36 /** only has meaning if a boundary is used */
37 private int maximumPoolSize = 150;
38
39 /**
40 * the exact number that will be used in a boundless queue. If the queue has a boundary, more
41 * will be created if the queue fills.
42 */
43 private int minimumPoolSize = 4;
44
45 /** How long idle threads above the minimum should be kept alive. */
46 private int keepAliveTime = 1000 * 60 * 5;
47
48 public enum WhenBlockedPolicy {
49 /** abort when queue is full and max threads is reached. */
50 ABORT,
51
52 /** block when queue is full and max threads is reached. */
53 BLOCK,
54
55 /** run in current thread when queue is full and max threads is reached. */
56 RUN,
57
58 /** wait when queue is full and max threads is reached. */
59 WAIT,
60
61 /** discard oldest when queue is full and max threads is reached. */
62 DISCARDOLDEST
63 }
64
65 /** should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */
66 private WhenBlockedPolicy whenBlockedPolicy = WhenBlockedPolicy.RUN;
67
68 /** The number of threads to create on startup */
69 private int startUpSize = 4;
70
71 /**
72 * @param useBoundary The useBoundary to set.
73 */
74 public void setUseBoundary( boolean useBoundary )
75 {
76 this.useBoundary = useBoundary;
77 }
78
79 /**
80 * @return Returns the useBoundary.
81 */
82 public boolean isUseBoundary()
83 {
84 return useBoundary;
85 }
86
87 /**
88 * Default
89 */
90 public PoolConfiguration()
91 {
92 // nop
93 }
94
95 /**
96 * Construct a completely configured instance.
97 * <p>
98 * @param useBoundary
99 * @param boundarySize
100 * @param maximumPoolSize
101 * @param minimumPoolSize
102 * @param keepAliveTime
103 * @param whenBlockedPolicy
104 * @param startUpSize
105 */
106 public PoolConfiguration( boolean useBoundary, int boundarySize, int maximumPoolSize, int minimumPoolSize,
107 int keepAliveTime, WhenBlockedPolicy whenBlockedPolicy, int startUpSize )
108 {
109 setUseBoundary( useBoundary );
110 setBoundarySize( boundarySize );
111 setMaximumPoolSize( maximumPoolSize );
112 setMinimumPoolSize( minimumPoolSize );
113 setKeepAliveTime( keepAliveTime );
114 setWhenBlockedPolicy( whenBlockedPolicy );
115 setStartUpSize( startUpSize );
116 }
117
118 /**
119 * @param boundarySize The boundarySize to set.
120 */
121 public void setBoundarySize( int boundarySize )
122 {
123 this.boundarySize = boundarySize;
124 }
125
126 /**
127 * @return Returns the boundarySize.
128 */
129 public int getBoundarySize()
130 {
131 return boundarySize;
132 }
133
134 /**
135 * @param maximumPoolSize The maximumPoolSize to set.
136 */
137 public void setMaximumPoolSize( int maximumPoolSize )
138 {
139 this.maximumPoolSize = maximumPoolSize;
140 }
141
142 /**
143 * @return Returns the maximumPoolSize.
144 */
145 public int getMaximumPoolSize()
146 {
147 return maximumPoolSize;
148 }
149
150 /**
151 * @param minimumPoolSize The minimumPoolSize to set.
152 */
153 public void setMinimumPoolSize( int minimumPoolSize )
154 {
155 this.minimumPoolSize = minimumPoolSize;
156 }
157
158 /**
159 * @return Returns the minimumPoolSize.
160 */
161 public int getMinimumPoolSize()
162 {
163 return minimumPoolSize;
164 }
165
166 /**
167 * @param keepAliveTime The keepAliveTime to set.
168 */
169 public void setKeepAliveTime( int keepAliveTime )
170 {
171 this.keepAliveTime = keepAliveTime;
172 }
173
174 /**
175 * @return Returns the keepAliveTime.
176 */
177 public int getKeepAliveTime()
178 {
179 return keepAliveTime;
180 }
181
182 /**
183 * @param whenBlockedPolicy The whenBlockedPolicy to set.
184 */
185 public void setWhenBlockedPolicy( String whenBlockedPolicy )
186 {
187 if ( whenBlockedPolicy != null )
188 {
189 WhenBlockedPolicy policy = WhenBlockedPolicy.valueOf(whenBlockedPolicy.trim().toUpperCase());
190 setWhenBlockedPolicy(policy);
191 }
192 else
193 {
194 // the value is null, default to RUN
195 this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
196 }
197 }
198
199 /**
200 * @param whenBlockedPolicy The whenBlockedPolicy to set.
201 */
202 public void setWhenBlockedPolicy( WhenBlockedPolicy whenBlockedPolicy )
203 {
204 if ( whenBlockedPolicy != null )
205 {
206 this.whenBlockedPolicy = whenBlockedPolicy;
207 }
208 else
209 {
210 // the value is null, default to RUN
211 this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
212 }
213 }
214
215 /**
216 * @return Returns the whenBlockedPolicy.
217 */
218 public WhenBlockedPolicy getWhenBlockedPolicy()
219 {
220 return whenBlockedPolicy;
221 }
222
223 /**
224 * @param startUpSize The startUpSize to set.
225 */
226 public void setStartUpSize( int startUpSize )
227 {
228 this.startUpSize = startUpSize;
229 }
230
231 /**
232 * @return Returns the startUpSize.
233 */
234 public int getStartUpSize()
235 {
236 return startUpSize;
237 }
238
239 /**
240 * To string for debugging purposes.
241 * @return String
242 */
243 @Override
244 public String toString()
245 {
246 StringBuilder buf = new StringBuilder();
247 buf.append( "useBoundary = [" + isUseBoundary() + "] " );
248 buf.append( "boundarySize = [" + boundarySize + "] " );
249 buf.append( "maximumPoolSize = [" + maximumPoolSize + "] " );
250 buf.append( "minimumPoolSize = [" + minimumPoolSize + "] " );
251 buf.append( "keepAliveTime = [" + keepAliveTime + "] " );
252 buf.append( "whenBlockedPolicy = [" + getWhenBlockedPolicy() + "] " );
253 buf.append( "startUpSize = [" + startUpSize + "]" );
254 return buf.toString();
255 }
256
257 /**
258 * Copies the instance variables to another instance.
259 * <p>
260 * @return PoolConfiguration
261 */
262 @Override
263 public PoolConfiguration clone()
264 {
265 return new PoolConfiguration( isUseBoundary(), boundarySize, maximumPoolSize, minimumPoolSize, keepAliveTime,
266 getWhenBlockedPolicy(), startUpSize );
267 }
268 }