Skip to content

Commit b40f881

Browse files
committed
1.0.137
1 parent 8ad409e commit b40f881

File tree

6 files changed

+341
-4
lines changed

6 files changed

+341
-4
lines changed

hy.common.base-sources.jar

1.99 KB
Binary file not shown.

hy.common.base.jar

1.86 KB
Binary file not shown.

src/MANIFEST.MF

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Manifest-Version: 1.0.136
1+
Manifest-Version: 1.0.137
22
Created-By: [email protected] 2008
3-
Specification-Version: 1.0.136 2018-06-19
3+
Specification-Version: 1.0.137 2018-07-03
44
SourceCode: https://github.com/HY-ZhengWei/hy.common.base
55
Build-JDK: 1.6
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=1.0.136
1+
version=1.0.137
22
groupId=org
33
artifactId=hy.common.base

src/META-INF/maven/org/hy/common/base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org</groupId>
66
<artifactId>hy.common.base</artifactId>
7-
<version>1.0.136</version>
7+
<version>1.0.137</version>
88
<packaging>jar</packaging>
99

1010
<name>hy.common.base</name>

src/org/hy/common/QueuePool.java

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
package org.hy.common;
2+
3+
import java.util.concurrent.ConcurrentLinkedQueue;
4+
5+
6+
7+
8+
9+
/**
10+
* 队列缓存池。
11+
*
12+
* 预先创建对象(主要用于对象在创建时十分耗时的情况),在get()时直接使用,提高创建对象的性能。
13+
*
14+
* 创建对象的时机:
15+
* 1. 构造本类的实例时。可通过i_IsInitPool参数调用创建对象的方式(true:同步创建对象; false:异步创建对象)。
16+
* 2. 调用get()方法时。当队列缓存池中的元素小于poolMinSize阀值时,将自动的、异步的、多线程的创建对象。
17+
*
18+
* 测试数据:创建1000个Fel用时23秒,但1000次计算只需0.200秒。
19+
*
20+
* @author ZhengWei(HY)
21+
* @createDate 2018-07-02
22+
* @version v1.0
23+
* @param <O> 队列缓存池中预先创建的对象
24+
*/
25+
public class QueuePool<O> extends ConcurrentLinkedQueue<O>
26+
{
27+
28+
private static final long serialVersionUID = -8087291628889068333L;
29+
30+
31+
/** 队列缓存池中的元素类型 */
32+
private Class<O> poolDataClass;
33+
34+
/** 队列缓存池的大小 */
35+
private int poolSize;
36+
37+
/** 队列缓存池的最小大小,当小于此值时,将创建新的元素并添加到池中 */
38+
private int poolMinSize;
39+
40+
/** 创建队列缓存池中元素的最大线程数量(默认值:10) */
41+
private int addingMaxThreadCount;
42+
43+
/** 创建队列缓存池中元素的当前程数量 */
44+
private int addingThreadCount;
45+
46+
47+
48+
/**
49+
* 队列缓存池中的构造器
50+
*
51+
* @author ZhengWei(HY)
52+
* @createDate 2018-07-02
53+
* @version v1.0
54+
*
55+
* @param i_PoolDataClass 队列缓存池中的元素类型
56+
* @param i_PoolSize 队列缓存池的大小
57+
* @param i_PoolMinSize 队列缓存池的最小大小,当小于此值时,将创建新的元素并添加到池中
58+
*/
59+
public QueuePool(Class<O> i_PoolDataClass ,int i_PoolSize ,int i_PoolMinSize)
60+
{
61+
this(i_PoolDataClass ,i_PoolSize ,i_PoolMinSize ,true);
62+
}
63+
64+
65+
66+
/**
67+
* 队列缓存池中的构造器
68+
*
69+
* @author ZhengWei(HY)
70+
* @createDate 2018-07-02
71+
* @version v1.0
72+
*
73+
* @param i_PoolDataClass 队列缓存池中的元素类型
74+
* @param i_PoolSize 队列缓存池的大小
75+
* @param i_PoolMinSize 队列缓存池的最小大小,当小于此值时,将创建新的元素并添加到池中
76+
* @param i_IsInitPool 在构造器中初始化完成队列缓存池
77+
*/
78+
public QueuePool(Class<O> i_PoolDataClass ,int i_PoolSize ,int i_PoolMinSize ,boolean i_IsInitPool)
79+
{
80+
this.poolDataClass = i_PoolDataClass;
81+
this.poolSize = i_PoolSize;
82+
this.poolMinSize = i_PoolMinSize;
83+
this.addingMaxThreadCount = 10;
84+
this.addingThreadCount = 0;
85+
86+
if ( this.poolDataClass == null )
87+
{
88+
throw new InstantiationError("Pool data class is null.");
89+
}
90+
this.setPoolSize (i_PoolSize);
91+
this.setPoolMinSize(i_PoolMinSize);
92+
93+
if ( i_IsInitPool )
94+
{
95+
this.adding();
96+
}
97+
else
98+
{
99+
this.startAdding();
100+
}
101+
}
102+
103+
104+
105+
/**
106+
* 从队列缓存池中获取一个新的元素。
107+
*
108+
* 当队列缓存池中不存元素时,临时创建一个新元素返回。
109+
*
110+
* 注:有意不加同步锁synchronized,好处是:在队列缓存中元素不足时,有机会产生多个线程同时创建新元素。
111+
*
112+
* @author ZhengWei(HY)
113+
* @createDate 2018-07-02
114+
* @version v1.0
115+
*
116+
* @return
117+
*
118+
* @see org.hy.common.Queue#get()
119+
*/
120+
public O get()
121+
{
122+
O v_Ret = this.poll();
123+
124+
if ( v_Ret == null )
125+
{
126+
v_Ret = this.newObject();
127+
}
128+
129+
if ( this.size() <= this.poolMinSize )
130+
{
131+
this.startAdding();
132+
}
133+
134+
return v_Ret;
135+
}
136+
137+
138+
139+
/**
140+
* 开始异步方式执行向队列缓存池中添加元素。
141+
*
142+
* @author ZhengWei(HY)
143+
* @createDate 2018-07-02
144+
* @version v1.0
145+
*
146+
*/
147+
public void startAdding()
148+
{
149+
(new Execute(this ,"adding")).start();
150+
}
151+
152+
153+
154+
/**
155+
* 向队列缓存池中添加元素。
156+
*
157+
* 注:有意不加同步锁synchronized,好处是:在队列缓存中元素不足时,有机会产生多个线程同时创建新元素。
158+
*
159+
* @author ZhengWei(HY)
160+
* @createDate 2018-07-02
161+
* @version v1.0
162+
*
163+
*/
164+
public void adding()
165+
{
166+
if ( addingThreadCount() )
167+
{
168+
try
169+
{
170+
while ( this.size() < this.poolSize )
171+
{
172+
this.add(this.newObject());
173+
}
174+
}
175+
finally
176+
{
177+
this.addingFinsh();
178+
}
179+
}
180+
}
181+
182+
183+
184+
/**
185+
* 添加线程计数器
186+
*
187+
* @author ZhengWei(HY)
188+
* @createDate 2018-07-03
189+
* @version v1.0
190+
*
191+
* @return
192+
*/
193+
private synchronized boolean addingThreadCount()
194+
{
195+
if ( this.addingThreadCount > this.addingMaxThreadCount )
196+
{
197+
return false;
198+
}
199+
else
200+
{
201+
this.addingThreadCount++;
202+
return true;
203+
}
204+
}
205+
206+
207+
private synchronized void addingFinsh()
208+
{
209+
this.addingThreadCount--;
210+
}
211+
212+
213+
214+
/**
215+
* 创建队列缓存池中的元素对象
216+
*
217+
* @author ZhengWei(HY)
218+
* @createDate 2018-07-02
219+
* @version v1.0
220+
*
221+
* @return
222+
*/
223+
public O newObject()
224+
{
225+
try
226+
{
227+
return this.poolDataClass.newInstance();
228+
}
229+
catch (Exception exce)
230+
{
231+
exce.printStackTrace();
232+
}
233+
234+
return null;
235+
}
236+
237+
238+
239+
/**
240+
* 获取:队列缓存池中的元素类型
241+
*/
242+
public Class<O> getPoolDataClass()
243+
{
244+
return poolDataClass;
245+
}
246+
247+
248+
249+
/**
250+
* 获取:队列缓存池的大小
251+
*/
252+
public int getPoolSize()
253+
{
254+
return poolSize;
255+
}
256+
257+
258+
259+
/**
260+
* 获取:队列缓存池的最小大小,当小于此值时,将创建新的元素并添加到池中
261+
*/
262+
public int getPoolMinSize()
263+
{
264+
return poolMinSize;
265+
}
266+
267+
268+
269+
/**
270+
* 设置:队列缓存池的大小
271+
*
272+
* @param i_PoolSize
273+
*/
274+
public void setPoolSize(int i_PoolSize)
275+
{
276+
if ( i_PoolSize < 1 )
277+
{
278+
throw new InstantiationError("Pool size < 1.");
279+
}
280+
281+
this.poolSize = i_PoolSize;
282+
283+
this.startAdding();
284+
}
285+
286+
287+
288+
/**
289+
* 设置:队列缓存池的最小大小,当小于此值时,将创建新的元素并添加到池中
290+
*
291+
* @param i_PoolMinSize
292+
*/
293+
public void setPoolMinSize(int i_PoolMinSize)
294+
{
295+
if ( i_PoolMinSize < 0 )
296+
{
297+
throw new InstantiationError("Pool min size < 0.");
298+
}
299+
300+
this.poolMinSize = i_PoolMinSize;
301+
302+
this.startAdding();
303+
}
304+
305+
306+
307+
/**
308+
* 获取:创建队列缓存池中元素的最大线程数量(默认值:10)
309+
*/
310+
public int getAddingMaxThreadCount()
311+
{
312+
return addingMaxThreadCount;
313+
}
314+
315+
316+
317+
/**
318+
* 设置:创建队列缓存池中元素的最大线程数量(默认值:10)
319+
*
320+
* @param addingMaxThreadCount
321+
*/
322+
public void setAddingMaxThreadCount(int addingMaxThreadCount)
323+
{
324+
this.addingMaxThreadCount = addingMaxThreadCount;
325+
}
326+
327+
328+
329+
/**
330+
* 获取:创建队列缓存池中元素的当前程数量
331+
*/
332+
public int getAddingThreadCount()
333+
{
334+
return addingThreadCount;
335+
}
336+
337+
}

0 commit comments

Comments
 (0)