In: Computer Science
When it is necessary to work with a large number of objects that are particularly expensive to instantiate and each object is only needed for a short period of time, the performance of an entire application may be adversely affected. An object pool design pattern may be deemed desirable in cases such as these. The object pool pattern is a design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it, allowing it to be used again in the future without repeating the computationally expensive instantiation process. It is important to note that once an object has been used and returned, existing references will become invalid.
When a new object is needed, it is requested from the pool. If a previously prepared object is available it is returned immediately, avoiding the instantiation cost. If no objects are present in the pool, a new item is created and returned. In some object pools the resources are limited so a maximum number of objects is specified. If this number is reached and a new item is requested, the thread will be blocked until an object is released back into the pool.
Object pools are primarily used for performance. For example, a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each client, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database.
Consider a dummy ServiceObject class that has a single state variable (String myState) and a single method doSomething() that just prints “myState”. Each client of ServiceObject will have its own “myState”
---------------------------------------------------------------------------ANSWER----------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;
public class ObjectPool<T>
{
private Set<T>available = new HashSet<>();
private Set<T>inUse= new HashSet<>();
private int MAXTotalObjects;
private int counter = 0;
private ObjectPool<T>instance = null;
public ObjectPool<T>GetInstance()
{
if (instance == null)
{
instance = new ObjectPool<T>();
}
else
{
System.out.print("This is singleton!");
}
return instance;
}
public T getInstance() {
if (available.size() != 0 &&available.size() <=
10)
{
T instance = available.iterator().next();
available.remove(instance);
inUse.add(instance);
counter--;
return instance;
}
else
{
T obj = (T) new ServiceObject();
inUse.add(obj);
return obj;
}
}
public booleanreleaseInstance(T instance) {
if (counter <= MAXTotalObjects)
{
available.add(instance);
counter++;
inUse.remove(instance);
}
else
{
System.out.println("To much object in pool!");
}
return false;
}
public void SetMaxPoolSize(int settingPoolSize)
{
MAXTotalObjects= settingPoolSize;
}
@Override
public String toString() {
return String.format("Pool available=%d inUse=%d",
available.size(), inUse.size());
}
}
public class ServiceObject {
private String myState;
public ServiceObject()
{
}
public ServiceObject(String myState) {
this.myState= myState;
}
public String getMyState() {
return myState;
}
public void setMyState(String myState) {
this.myState= myState;
}
public String doSomething() {
return myState;
}
}
public class PoolTest {
public static void main(String[] args)
{
ObjectPool<ServiceObject>objPool = new
ObjectPool<ServiceObject>().GetInstance();
objPool.SetMaxPoolSize(5);
ServiceObject obj = objPool.getInstance();
ServiceObject obj1 = objPool.getInstance();
ServiceObject obj2 = objPool.getInstance();
ServiceObject obj3 = objPool.getInstance();
ServiceObject obj4 = objPool.getInstance();
ServiceObject obj5 = objPool.getInstance();
ServiceObject obj6 = objPool.getInstance();
ServiceObject obj7 = objPool.getInstance();
ServiceObject obj8 = objPool.getInstance();
ServiceObject obj9 = objPool.getInstance();
ServiceObject obj10 = objPool.getInstance();
ServiceObject obj11 = objPool.getInstance();
System.out.println(objPool.toString());
objPool.releaseInstance(obj);
objPool.releaseInstance(obj1);
objPool.releaseInstance(obj2);
objPool.releaseInstance(obj3);
objPool.releaseInstance(obj4);
objPool.releaseInstance(obj5);
objPool.releaseInstance(obj6);
objPool.releaseInstance(obj7);
objPool.releaseInstance(obj8);
System.out.println(objPool.toString());
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------