Package net.i2p.util

Class TryCache<T>

  • Type Parameters:
    T -
    Direct Known Subclasses:
    ByteCache

    public class TryCache<T>
    extends Object
    An object cache which is safe to use by multiple threads without blocking.
    Since:
    0.9.36
    Author:
    zab
    • Field Detail

      • capacity

        protected final int capacity
      • items

        protected final List<T> items
      • lock

        protected final Lock lock
      • _lastUnderflow

        protected long _lastUnderflow
    • Constructor Detail

      • TryCache

        public TryCache​(TryCache.ObjectFactory<T> factory,
                        int capacity)
        Parameters:
        factory - to be used for creating new instances
        capacity - cache up to this many items
    • Method Detail

      • acquire

        public T acquire()
        Returns:
        a cached or newly created item from this cache
      • release

        public void release​(T item)
        Tries to return this item to the cache but it may fail if the cache has reached capacity or it's lock is held by another thread.
      • clear

        public void clear()
        Clears all cached items. This is the only method that blocks until it acquires the lock.