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

org.nassimus.thread.util.SimpleObjectPool Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package org.nassimus.thread.util;

import java.util.HashMap;
import java.util.Map;

public abstract class SimpleObjectPool {
    private final int maxNbObjects;
    private final Map objects;
    private T[] objectsArray;
    public SimpleObjectPool(int maxNbObjects){
        this.maxNbObjects = maxNbObjects;
        objects = new HashMap<>(maxNbObjects);
    }

    public abstract T createAnObject();

    public T checkOut() {
        if (objectsArray!=null)
            for (T object : objectsArray) {
                Boolean state = objects.get(object);
                if (state!=null && !state) {
                    objects.put(object, true);
                    return object;
                }
            }
        T object = createAnObject();
        objects.put(object, true);
        synchronized (this) {
            objectsArray = (T[]) objects.keySet().toArray();
        }
        return object;
    }
    public void release(T object) {
        synchronized (this){
            objects.put(object, false);
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy