Changed to sleep(x), instead of yield(). Added 'createFast' and 'createSafe' to factory

This commit is contained in:
nathan 2015-12-19 02:09:42 +01:00
parent 20c26edc2f
commit ea25dedff6
2 changed files with 34 additions and 1 deletions

View File

@ -38,4 +38,34 @@ class ObjectPoolFactory {
return slowObjectPool;
}
}
/**
* Creates an UNSAFE pool of the specified size, rounded up to the nearest power of 2.
*/
public static
<T> ObjectPool<T> createFast(PoolableObject<T> poolableObject, int size) {
// here we use FAST (via UNSAFE)
UnsafeObjectPool<T> fastObjectPool = null;
try {
fastObjectPool = new UnsafeObjectPool<T>(poolableObject, size);
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException("Unable to create a fast object pool, use 'create()' instead. Aborting.", e);
}
return fastObjectPool;
}
/**
* Creates a SAFE pool of the specified size.
*/
public static
<T> ObjectPool<T> createSafe(PoolableObject<T> poolableObject, int size) {
// fallback (LinkedBlockingDeque) in case UNSAFE isn't available. (ie: android)
SafeObjectPool<T> slowObjectPool = new SafeObjectPool<T>(poolableObject, size);
return slowObjectPool;
}
}

View File

@ -90,7 +90,10 @@ class UnsafeObjectPool<T> implements ObjectPool<T> {
if (!objects.offer(object)) {
int limit = FULL_RETRY_LIMIT;
while (!objects.offer(object) && limit-- > 0) {
Thread.yield();
try {
Thread.sleep(2L);
} catch (InterruptedException ignored) {
}
}
if (limit <= 0) {