Added support for initial capacity pre-init to Pool

This commit is contained in:
Collin Smith 2020-09-22 13:35:23 -07:00
parent 63c17b0e17
commit 5c10340222
4 changed files with 19 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import com.riiablo.util.Pool;
public final class Logger {
private static final String FQCN = Logger.class.getName();
private static final Pool<LogEvent> POOL = new Pool<LogEvent>(true, true) {
private static final Pool<LogEvent> POOL = new Pool<LogEvent>(true, true, 512, Integer.MAX_VALUE) {
@Override
protected LogEvent newInstance() {
return new LogEvent();

View File

@ -7,7 +7,7 @@ import java.util.Locale;
import com.riiablo.util.Pool;
public class PooledFormattedMessage implements Message {
static final Pool<PooledFormattedMessage> POOL = new Pool<PooledFormattedMessage>(true, true) {
static final Pool<PooledFormattedMessage> POOL = new Pool<PooledFormattedMessage>(true, true, 256, Integer.MAX_VALUE) {
@Override
protected PooledFormattedMessage newInstance() {
return new PooledFormattedMessage();

View File

@ -6,7 +6,7 @@ import com.riiablo.util.Pool;
public class PooledParameterizedMessage implements Message {
private static final int MAX_BUFFER_SIZE = 255;
private static final Pool<StringBuilder> BUFFER_POOL = new Pool<StringBuilder>(true, true) {
private static final Pool<StringBuilder> BUFFER_POOL = new Pool<StringBuilder>(true, true, 256, Integer.MAX_VALUE) {
@Override
protected StringBuilder newInstance() {
return new StringBuilder(MAX_BUFFER_SIZE);
@ -26,7 +26,7 @@ public class PooledParameterizedMessage implements Message {
BUFFER_POOL.release(buffer);
}
static final Pool<PooledParameterizedMessage> POOL = new Pool<PooledParameterizedMessage>(true, true) {
static final Pool<PooledParameterizedMessage> POOL = new Pool<PooledParameterizedMessage>(true, true, 256, Integer.MAX_VALUE) {
@Override
protected PooledParameterizedMessage newInstance() {
return new PooledParameterizedMessage();

View File

@ -19,8 +19,16 @@ public abstract class Pool<T> {
this(threadSafe, softReferences, Integer.MAX_VALUE);
}
@SuppressWarnings("unchecked")
public Pool(boolean threadSafe, boolean softReferences, final int maxCapacity) {
this(threadSafe, softReferences, maxCapacity, 0);
}
@SuppressWarnings("unchecked")
public Pool(boolean threadSafe, boolean softReferences, final int initialCapacity, final int maxCapacity) {
if (initialCapacity >= maxCapacity) {
throw new IllegalArgumentException("initialCapacity(" + initialCapacity + ") >= maxCapacity(" + maxCapacity + ")");
}
final Queue<T> queue;
if (threadSafe) {
queue = new LinkedBlockingQueue<T>(maxCapacity) {
@ -54,6 +62,12 @@ public abstract class Pool<T> {
freeObjects = softReferences
? new SoftReferenceQueue<>((Queue<SoftReference<T>>) queue)
: queue;
if (initialCapacity > 0) {
for (int i = 0; i < initialCapacity; i++) {
release(newInstance());
}
}
}
protected abstract T newInstance();