Changed to sleep(x), instead of yield(). Added 'createFast' and 'createSafe' to factory
This commit is contained in:
parent
20c26edc2f
commit
ea25dedff6
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue