Updated objectPool to support "reset" for objects returned to the pool.

This commit is contained in:
nathan 2015-08-22 23:43:53 +02:00
parent 1d7c3637a6
commit 29c5ac4563
5 changed files with 45 additions and 18 deletions

View File

@ -8,18 +8,8 @@
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="JCTools (1.1 alpha MTAQ)" level="application" />
<orderEntry type="library" name="junit-4.12" level="application" />
</component>
<component name="org.twodividedbyzero.idea.findbugs">
<option name="_basePreferences">

View File

@ -26,13 +26,12 @@ interface ObjectPool<T> {
/**
* Takes an object from the pool, Blocks until an item is available in the pool.
* <p/>
* This method catches an
* {@link InterruptedException} and discards it silently.
* This method catches {@link InterruptedException} and discards it silently.
*/
T takeUninterruptibly();
/**
* Return object to the pool, waking those that have blocked during take()
* Return object to the pool, waking those threads that have blocked during take()
*/
void release(T object);
@ -40,4 +39,9 @@ interface ObjectPool<T> {
* @return a new object instance created by the pool.
*/
T newInstance();
/**
* @return the number of pooled objects
*/
int size();
}

View File

@ -16,9 +16,20 @@
package dorkbox.util.objectPool;
public
interface PoolableObject<T> {
abstract class PoolableObject<T> {
/**
* Called when an object is returned to the pool, useful for resetting an objects state, for example.
* @param object
*/
@SuppressWarnings("UnusedParameters")
public
void reset(T object) {
}
/**
* called when a new instance is created
*/
T create();
public abstract T create();
}

View File

@ -32,7 +32,9 @@ class SafeObjectPool<T> implements ObjectPool<T> {
this.queue = new ArrayBlockingQueue<T>(size);
for (int x = 0; x < size; x++) {
this.queue.add(poolableObject.create());
T e = poolableObject.create();
poolableObject.reset(e);
this.queue.add(e);
}
}
@ -55,6 +57,7 @@ class SafeObjectPool<T> implements ObjectPool<T> {
@Override
public
void release(T object) {
poolableObject.reset(object);
this.queue.offer(object);
}
@ -63,4 +66,10 @@ class SafeObjectPool<T> implements ObjectPool<T> {
T newInstance() {
return poolableObject.create();
}
@Override
public
int size() {
return queue.size();
}
}

View File

@ -42,7 +42,9 @@ class UnsafeObjectPool<T> implements ObjectPool<T> {
objects = new MpmcArrayQueue<T>(newSize);
for (int x = 0; x < newSize; x++) {
objects.offer(poolableObject.create());
T e = poolableObject.create();
poolableObject.reset(e);
objects.offer(e);
}
}
@ -77,6 +79,7 @@ class UnsafeObjectPool<T> implements ObjectPool<T> {
@Override
public
void release(T object) {
poolableObject.reset(object);
boolean waiting = objects.peek() == null;
// This could potentially happen due to optimistic calculations by the implementation queue.
@ -110,4 +113,14 @@ class UnsafeObjectPool<T> implements ObjectPool<T> {
T newInstance() {
return poolableObject.create();
}
/**
* This is an optimistic calculation, and if being used by multiple threads, can be inaccurate.
*/
@Override
public
int size() {
return objects.size();
}
}