Instantiating with Pool <T> via PoolObjectFactory Interface

Below is an example of using Java pool (generics pool) to create TouchEvents for Android:

import java.util.ArrayList;
import java.util.List;

public class Pool<T> {
    public interface PoolObjectFactory<T> {
        public T createObject();
    }

    private final List<T> freeObjects;
    private final PoolObjectFactory<T> factory;
    private final int maxSize;

    public Pool(PoolObjectFactory<T> factory, int maxSize) {
        this.factory = factory;
        this.maxSize = maxSize;
        this.freeObjects = new ArrayList<T>(maxSize);
    }

    public T newObject() {
        T object = null;

        if (freeObjects.isEmpty()) {
            object = factory.createObject();
        } else {
            object = freeObjects.remove(freeObjects.size() - 1);
        }

        return object;
    }

    public void free(T object) {
        if (freeObjects.size() < maxSize) {
            freeObjects.add(object);
        }
    }
}

      

However, I really don't understand how this code works:

if (freeObjects.isEmpty()) {
    object = factory.createObject();
} else {
    object = freeObjects.remove(freeObjects.size() - 1);
}

      

Let's say we have:

touchEventPool = new Pool<TouchEvent>(factory, 100);

      

Does this mean it is going to store an Array of 100 events (and when # 101 comes in, it will position # 1 as first-in-first-out)?

My guess is that it should contain a certain number of objects and then remove unnecessary ones. I read the book description like 10 times .. and couldn't get it. Maybe someone will explain how it works?

+3


source to share


3 answers


My guess is that it should contain a certain number of objects and then remove unnecessary ones. I read the book description like 10 times .. and couldn't get it. Maybe someone will explain how it works?

Grade. The class stores a cache of pre-built objects in a List

called pool

. When you request a new object (using a method newObject

), it will first check pool

to see if the object is available for use. If the pool is empty, it just creates an object and returns it to you. If there is an available object, it removes the last item in pool

and returns it to you.

Annotated:

if (freeObjects.isEmpty()) {
    // The pool is empty, create a new object.
    object = factory.createObject();
} else {
    // The pool is non-empty, retrieve an object from the pool and return it.
    object = freeObjects.remove(freeObjects.size() - 1);
}

      



And when you return an object to the cache (using a method free()

), it will be pushed back to the pool if the maximum pool size has not been met.

Annotated:

public void free(T object) {
    // If the pool is not already at its maximum size.
    if (freeObjects.size() < maxSize) {
        // Then put the object into the pool.
        freeObjects.add(object);
    }
    // Otherwise, just ignore this call and let the object go out of scope.
}

      

If the maximum pool size has already been reached, the object you are freeing is not saved and is presumably subject to garbage collection.

+3


source


The idea behind any pool is to create a controlled environment where (usually) there is no need to create new (events) instances, where some unused free instances can be reused from the pool.

When you create touchEventPool = new Pool<TouchEvent>(factory, 100);

you hope that 100 copies will be enough at any given point in the live program.



So, when you want to receive the 101st events, the process will probably release the first 5, 20, or even 99 events and the pool can reuse any of them.

If there are no free instances, then depending on the pool policy, a new one will be created, or the request thread will wait for other threads to release and return to the pool. In this particular implementation, a new one will be created.

+1


source


I think the main concept of object pooling is to reduce the frequency of object initializations.

Does this mean it is going to store an array of 100 events (and when # 101 goes inside, will it be # 1 like the first one in first-out)? Does this mean that it is going to store an array of 100 events (and when # 101 comes in, it will position # 1 as first-in-first-out)?

I do not think so. A maximum of 100 means freeObjects, but currently uses objects. When the object is no longer in use, you free it. Then the freed object will not be archived, but will be patented as freeObject (the maximum number means that of these objects saved). The next time you need another new object, you don't need to create a new object. All you need is simply to reuse one of the spare free objects.

This way, you can avoid costly objects. It can improve performance.

+1


source







All Articles