Class ConcurrentCachedGenerator<T>
- java.lang.Object
-
- dev.nm.stat.random.rng.concurrent.cache.ConcurrentCachedGenerator<T>
-
- Type Parameters:
T
- the type of the item
- All Implemented Interfaces:
Seedable
public class ConcurrentCachedGenerator<T> extends Object implements Seedable
A generic wrapper that makes an underlying item generator thread-safe by caching generated items in a concurrently-accessible list. This is more efficient than plainly synchronizing all accessors of the generator.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ConcurrentCachedGenerator.Generator<T>
Defines a generic generator of typeT
.
-
Constructor Summary
Constructors Constructor Description ConcurrentCachedGenerator(ConcurrentCachedGenerator.Generator<T> generator, int cacheSize)
Creates a new instance which wraps the given item generator and uses a cache of the specified size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
next()
Returns the next value in the generated sequence.void
seed(long... seeds)
Seed the random number/vector/scenario generator to produce repeatable experiments.
-
-
-
Constructor Detail
-
ConcurrentCachedGenerator
public ConcurrentCachedGenerator(ConcurrentCachedGenerator.Generator<T> generator, int cacheSize)
Creates a new instance which wraps the given item generator and uses a cache of the specified size. A larger cache will make the concurrency slightly faster at the expense of increased memory usage, but may lead to the computation of unnecessary values at the tail.- Parameters:
generator
- the underlying generatorcacheSize
- the size of the cache
-
-
Method Detail
-
next
public T next()
Returns the next value in the generated sequence.- Returns:
- the next generated value
-
-