Package net.i2p.util
Class TryCache<T>
java.lang.Object
net.i2p.util.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
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TryCache.ObjectFactory<T>
Something that creates objects of the type cached by this cache -
Field Summary
-
Constructor Summary
Constructors Constructor Description TryCache(TryCache.ObjectFactory<T> factory, int capacity)
-
Method Summary
-
Field Details
-
capacity
protected final int capacity -
items
-
lock
-
_lastUnderflow
protected long _lastUnderflow
-
-
Constructor Details
-
TryCache
- Parameters:
factory
- to be used for creating new instancescapacity
- cache up to this many items
-
-
Method Details
-
acquire
- Returns:
- a cached or newly created item from this cache
-
release
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.
-