com.clickhouse.client.api.internal.BasicObjectsPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client-v2 Show documentation
Show all versions of client-v2 Show documentation
New client api for ClickHouse
package com.clickhouse.client.api.internal;
import java.util.Deque;
/**
* Objects pool.
* Can be used to reuse object to reduce GC pressure.
* Thread-safety is not guaranteed. Depends on deque implementation that is
* passed to the constructor.
* @param - type of stored objects
*/
public abstract class BasicObjectsPool {
private Deque objects;
/**
* Creates an empty pool.
* @param objects object queue
*/
BasicObjectsPool(Deque objects) {
this(objects, 0);
}
/**
* Creates a pool and pre-populates it with a number of objects equal to the size parameter.
* @param objects object queue
* @param size initial size of the object queue
*/
public BasicObjectsPool(Deque objects, int size) {
this.objects = objects;
for (int i = 0; i < size; i++) {
this.objects.add(create());
}
}
public T lease() {
T obj = objects.poll();
return obj != null ? obj : create();
}
public void release(T obj) {
objects.addFirst(obj);
}
protected abstract T create();
}