definition
An object pool is a collection of objects that have been initialized and usable. The user of the pool can obtain objects from the pool, operate on them, and return them to the pool instead of destroying it if not.
If the initialization and instantiation are expensive and there is a need to be instantiated frequently, but the number of instantiations is small at a time, significant performance improvements can be achieved using object pools. The time to get an object from the pool is predictable, but the time it takes to create a new instance is uncertain.
accomplish
1. Reusable - Objects in the object pool, usually the cost of instantiation is relatively high.
2. Client - Use an instance of an object.
3. ReusablePool - manages the instantiation, recycling and destruction of objects.
The main ideas in a single instance
1. A stack, here is stack
2. Initialization method, you can pre-create the pool when the container is opened.
3. Methods to create instances
4. Provide a method to obtain object instances from the pool
5. Provide a return method, but the consequences are very serious.
6. Method to control request waiting time. After a certain event has passed, a null pointer is returned.
import java.util.Stack; @SuppressWarnings("unchecked") public class ObjectPool { public ObjectPool() { } private PoolParam poolParam; public void setPoolParam(PoolParam poolParam) { this.poolParam = poolParam; } // The current total number of objects private int currentNum = 0; private Class clazz; public void setClazz(Class clazz) { this.clazz = clazz; } // The stack is used to store objects and simulate a pool private Stack stack; public Stack getStack() { return stack; } public void setStack(Stack stack) { this.stack = stack; } // .................................................................. // waiting for timeout to expire private int timeWait = 0; // ........................................................................ // Create an object pool public void initialPool(PoolParam poolParam, Class clazz) { this.setPoolParam(poolParam); this.setClazz(clazz); stack = new Stack(); stack.clear(); // System.out.println("obj..pool is initial..."); // Generate the minimum number of objects to configure and push it into the stack try { for (int i = 0; i < poolParam.getMinObjectCount(); i++) { // Initialize the object pool according to poolParam stack.push(clazz.newInstance()); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } // Create a single object private Object createObj(Class clazz) { Object obj = null; try { obj = clazz.newInstance(); // System.out.println("a new one..."); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return obj; } // get method provided by the object pool public Object getInstance(){ // System.out.println(stack.size()); Object object = null; if (stack.size() == 0) { // If the length of the current stack is 0, and the total number of objects does not exceed the maximum defined number if ((currentNum + poolParam.getMinObjectCount()) < poolParam .getMaxObjectCount()) { // Create a new object object = this.createObj(clazz); // Number of objects +1 currentNum++; } else { synchronized (this) { try { waitme(this); } catch (Exception e) { e.printStackTrace(); } // After obtaining the notification, check that the stack is empty and gives the resource just released if (!stack.empty()) { object = stack.pop(); } } } } else if (stack.size() > 0) { object = stack.pop(); // System.out.println(stack.size()); } return object; } // Method to return object public void returnObj(Object obj) { if (clazz.isInstance(obj)) { stack.push(obj); synchronized (this) { notify(); } } else { System.out.println("this object can not push to stack!"); } } // Wait for recursive algorithm private void waitme(ObjectPool pool) { // Wait for technical control of 2s if (timeWait >= 2000) { System.out.println("jump up this step.."); timeWait = 0; return; } else { try { pool.wait(500); // Wait for the count to accumulate. . timeWait +=1000; System.out.println("waiting time to free obj.."); if (stack.empty()) { System.out.println("agian...."); waitme(pool); } } catch (InterruptedException e) { e.printStackTrace(); } } } }
Manage pool classes, this is not very difficult, it's good to synchronize it
@SuppressWarnings("unchecked") public class ObjectPoolManage { private ObjectPoolManage() { } private static ObjectPool pool; // Implement a singleton's obtaining method...Default public static synchronized ObjectPool getCacheObject(Class clazz) { if (null != pool) { return pool; } else { createObjectPool(null, clazz); return pool; } } // Implement a singleton's obtaining method...Custom public static synchronized ObjectPool getCacheObject(PoolParam p, Class clazz) { if (null != pool) { return pool; } else { createObjectPool(p, clazz); return pool; } } private static ObjectPool createObjectPool(PoolParam p, Class clazz) { pool = new ObjectPool(); if (null == p) { pool.initalPool(new PoolParam(5,10), clazz); } else { pool.initalPool(p, clazz); } return pool; } private static Class getclazz(){ Class clazz=null; try { clazz= Class.forName(ppp.getPropertyByName("objectPath")); } catch (ClassNotFoundException e) { e.printStackTrace(); } return clazz; } }
Related questions and implementations
1. The number of objects can be limited in the object pool. When the limit exceeds the limit, the object pool needs to return an exception or a null value to notify the customer.
2. In a multi-threaded environment, the checkout and checkin methods need to be synchronized.
3. Clean out expired objects regularly.