Skip to content

Instantly share code, notes, and snippets.

@xalexchen
Created March 27, 2014 03:31
Show Gist options
  • Save xalexchen/9799566 to your computer and use it in GitHub Desktop.
Save xalexchen/9799566 to your computer and use it in GitHub Desktop.
Pool implemenation
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