简单的对象池实现
来源:互联网 发布:手机淘宝如何卖二手货 编辑:程序博客网 时间:2024/06/04 00:57
package test;import java.util.Queue;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public abstract class ObjectPool<T>{ private Queue<T> pool; private ScheduledExecutorService executorService; /** * Creates the pool. * * @param minIdle minimum number of objects residing in the pool */ public ObjectPool(final int minIdle) { // initialize pool initialize(minIdle); } /** * Creates the pool. * * @param minIdle minimum number of objects residing in the pool * @param maxIdle maximum number of objects residing in the pool * @param validationInterval time in seconds for periodical checking of minIdle / maxIdle conditions in a separate thread. * When the number of objects is less than minIdle, missing instances will be created. * When the number of objects is greater than maxIdle, too many instances will be removed. */ public ObjectPool(final int minIdle, final int maxIdle, final long validationInterval) { // initialize pool initialize(minIdle); // check pool conditions in a separate thread executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleWithFixedDelay(new Runnable() { public void run() { int size = pool.size(); if (size < minIdle) { int sizeToBeAdded = minIdle - size; for (int i = 0; i < sizeToBeAdded; i++) { pool.add(createObject()); } } else if (size > maxIdle) { int sizeToBeRemoved = size - maxIdle; for (int i = 0; i < sizeToBeRemoved; i++) { pool.poll(); } } } }, validationInterval, validationInterval, TimeUnit.SECONDS); } /** * Gets the next free object from the pool. If the pool doesn't contain any objects, * a new object will be created and given to the caller of this method back. * * @return T borrowed object */ public T borrowObject() { T object; if ((object = pool.poll()) == null) { object = createObject(); } return object; } /** * Returns object back to the pool. * * @param object object to be returned */ public void returnObject(T object) { if (object == null) { return; } this.pool.offer(object); } /** * Shutdown this pool. */ public void shutdown() { if (executorService != null) { executorService.shutdown(); } } /** * Creates a new object. * * @return T new object */ protected abstract T createObject(); private void initialize(final int minIdle) { pool = new ConcurrentLinkedQueue<T>(); for (int i = 0; i < minIdle; i++) { pool.add(createObject()); } }}
0 0
- 简单的对象池实现
- C++实现简单的对象池
- 简单的对象池技术实现 DEMO
- 【Unity开发】简单的对象池实现
- Java对象池的简单实现
- 简单实现对象池
- C++通过list和template实现简单的对象池
- C++通过list和template实现简单的对象池
- 通过重载new和delete实现简单的对象池
- 通过重载new和delete实现简单的对象池
- PHP:简单实现ASP的Application对象
- 实现对象深拷贝的简单案例
- lua的简单对象实现以及简单继承
- 自己实现简单对象关系库之Database的实现
- 一个简单的对象池
- 一个简单的对象池
- 简单的内存对象池
- Golang简单的对象池
- 码农小汪-struts2学习3-struts2 配置文件说明
- 关于文件管理
- Shortest Distance from All Buildings
- Ext3.4 Column layout 设置form表单组件隐藏
- 关于size_t, ptrdiff_t, size_type, difference_type
- 简单的对象池实现
- 对象池的设计
- Asp.Net 关于FindControl方法使用
- googleCollection map 定时清除数据
- common,Google Guava,Guice
- Centos 6.5 更改系统时间
- Google guice
- commons.pool2 参数测试
- BVH文件 Java解析器