1 package org.apache.lucene.facet.search;
3 import java.util.concurrent.ConcurrentLinkedQueue;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * An TemporaryObjectAllocator is an object which manages large, reusable,
24 * temporary objects needed during multiple concurrent computations. The idea
25 * is to remember some of the previously allocated temporary objects, and
26 * reuse them if possible to avoid constant allocation and garbage-collection
29 * This technique is useful for temporary counter arrays in faceted search
30 * (see {@link FacetsAccumulator}), which can be reused across searches instead
31 * of being allocated afresh on every search.
33 * A TemporaryObjectAllocator is thread-safe.
35 * @lucene.experimental
37 public abstract class TemporaryObjectAllocator<T> {
39 // In the "pool" we hold up to "maxObjects" old objects, and if the pool
40 // is not empty, we return one of its objects rather than allocating a new
42 ConcurrentLinkedQueue<T> pool = new ConcurrentLinkedQueue<T>();
46 * Construct an allocator for objects of a certain type, keeping around a
47 * pool of up to <CODE>maxObjects</CODE> old objects.
49 * Note that the pool size only restricts the number of objects that hang
50 * around when not needed, but <I>not</I> the maximum number of objects
51 * that are allocated when actually is use: If a number of concurrent
52 * threads ask for an allocation, all of them will get an object, even if
53 * their number is greater than maxObjects. If an application wants to
54 * limit the number of concurrent threads making allocations, it needs to
55 * do so on its own - for example by blocking new threads until the
56 * existing ones have finished. If more than maxObjects are freed, only
57 * maxObjects of them will be kept in the pool - the rest will not and
58 * will eventually be garbage-collected by Java.
60 * In particular, when maxObjects=0, this object behaves as a trivial
61 * allocator, always allocating a new array and never reusing an old one.
63 public TemporaryObjectAllocator(int maxObjects) {
64 this.maxObjects = maxObjects;
68 * Subclasses must override this method to actually create a new object
69 * of the desired type.
72 protected abstract T create();
75 * Subclasses must override this method to clear an existing object of
76 * the desired type, to prepare it for reuse. Note that objects will be
77 * cleared just before reuse (on allocation), not when freed.
79 protected abstract void clear(T object);
82 * Allocate a new object. If there's a previously allocated object in our
83 * pool, we return it immediately. Otherwise, a new object is allocated.
85 * Don't forget to call {@link #free(Object)} when you're done with the object,
86 * to return it to the pool. If you don't, memory is <I>not</I> leaked,
87 * but the pool will remain empty and a new object will be allocated each
88 * time (just like the maxArrays=0 case).
90 public final T allocate() {
91 T object = pool.poll();
100 * Return a no-longer-needed object back to the pool. If we already have
101 * enough objects in the pool (maxObjects as specified in the constructor),
102 * the array will not be saved, and Java will eventually garbage collect
105 * In particular, when maxArrays=0, the given array is never saved and
108 public final void free(T object) {
109 if (pool.size() < maxObjects && object != null) {