All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickhouse.client.api.internal.BasicObjectsPool Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
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();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy