Created
March 27, 2014 03:31
-
-
Save xalexchen/9799566 to your computer and use it in GitHub Desktop.
Pool implemenation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static abstract class Pool<T> { | |
private final Deque<T> pool; | |
Pool(int initialSize) { | |
pool = new ArrayDeque<T>(initialSize); | |
for (int i = 0; i < initialSize; i++) { | |
pool.addLast(newObject()); | |
} | |
} | |
T obtain() { | |
return pool.isEmpty() ? newObject() : pool.removeLast(); | |
} | |
void restore(T instance) { | |
pool.addLast(instance); | |
} | |
protected abstract T newObject(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment