Class SimpleRandomSeeder
- java.lang.Object
-
- io.github.pr0methean.betterrandom.util.Looper
-
- io.github.pr0methean.betterrandom.seed.SimpleRandomSeeder
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
LegacyRandomSeeder
public class SimpleRandomSeeder extends Looper
Thread that loops overByteArrayReseedableRandominstances and reseeds them. NoEntropyCountingRandomwill be reseeded when it's already had more input than output.- Author:
- Chris Hennick
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimpleRandomSeeder.DefaultThreadFactoryAThreadFactorythat sets the name and priority of the threads it creates.
-
Field Summary
Fields Modifier and Type Field Description protected Set<ByteArrayReseedableRandom>byteArrayPrngsHoldsByteArrayReseedableRandominstances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom.protected Set<ByteArrayReseedableRandom>byteArrayPrngsThisIterationHolds instances that are being reseeded during the current iteration, so that PRNGs can be added and removed in the middle of an iteration without theConcurrentModificationExceptionthat would otherwise arise.protected static longDEFAULT_STOP_IF_EMPTY_FOR_NANOSDefault waiting time before an empty instance terminates if still empty.protected static longPOLL_INTERVALTime in seconds to wait before checking again whether any PRNGs need more entropy.protected SeedGeneratorseedGeneratorThe seed generator this seeder uses.protected longstopIfEmptyForNanosTime in nanoseconds after which this thread will terminate if no PRNGs are attached.protected ConditionwaitForEntropyDrainSignaled by an associatedBaseRandomwhen it runs out of entropy.protected ConditionwaitWhileEmptySignaled when a PRNG is added.-
Fields inherited from class io.github.pr0methean.betterrandom.util.Looper
DEFAULT_THREAD_FACTORY, factory, lock, thread, threadLock
-
-
Constructor Summary
Constructors Constructor Description SimpleRandomSeeder(SeedGenerator seedGenerator)Creates an instance using aSimpleRandomSeeder.DefaultThreadFactory.SimpleRandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory)Creates an instance whose thread will terminate if no PRNGs have been associated with it for 5 seconds.SimpleRandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory, long stopIfEmptyForNanos)Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(ByteArrayReseedableRandom... randoms)AddsByteArrayReseedableRandominstances.voidadd(Collection<? extends ByteArrayReseedableRandom> randoms)AddsByteArrayReseedableRandominstances.protected voidclear()Removes all PRNGs from this seeder.protected <T> Set<T>createSynchronizedWeakHashSet()Creates and returns a thread-safeSetbacked by aWeakHashMap.booleanequals(Object o)SeedGeneratorgetSeedGenerator()Returns theSeedGeneratorthis seeder uses.inthashCode()protected voidinitTransientFields()Initializes the transient instance fields for this class.booleanisEmpty()Returns true if noRandominstances are registered with this LegacyRandomSeeder.protected booleaniterate()The task that will be iterated until it returns false.voidremove(Collection<? extends Random> randoms)Removes PRNGs so that they will no longer be reseeded.voidremove(Random... randoms)Removes PRNGs so that they will no longer be reseeded.protected booleanreseedByteArrayReseedableRandoms()Reseeds all the PRNGs that need reseeding inbyteArrayPrngsThisIteration, then clears that set.protected voidreseedWithLong(Random random)Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long).voidshutDown()Shut down this thread even ifRandominstances are registered with it.voidstopIfEmpty()Shut down this thread if noRandominstances are registered with it.protected voidunregisterWithAll(Set<?> randoms)Informs the given PRNGs that they no longer have a seed generator.voidwakeUp()Ensures this seeder's thread is started, and signals conditions it may be waiting on.
-
-
-
Field Detail
-
POLL_INTERVAL
protected static final long POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy.- See Also:
- Constant Field Values
-
DEFAULT_STOP_IF_EMPTY_FOR_NANOS
protected static final long DEFAULT_STOP_IF_EMPTY_FOR_NANOS
Default waiting time before an empty instance terminates if still empty.- See Also:
- Constant Field Values
-
seedGenerator
protected final SeedGenerator seedGenerator
The seed generator this seeder uses.
-
byteArrayPrngs
protected transient Set<ByteArrayReseedableRandom> byteArrayPrngs
HoldsByteArrayReseedableRandominstances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom.
-
byteArrayPrngsThisIteration
protected transient Set<ByteArrayReseedableRandom> byteArrayPrngsThisIteration
Holds instances that are being reseeded during the current iteration, so that PRNGs can be added and removed in the middle of an iteration without theConcurrentModificationExceptionthat would otherwise arise.
-
waitWhileEmpty
protected transient Condition waitWhileEmpty
Signaled when a PRNG is added.
-
waitForEntropyDrain
protected transient Condition waitForEntropyDrain
Signaled by an associatedBaseRandomwhen it runs out of entropy.
-
stopIfEmptyForNanos
protected final long stopIfEmptyForNanos
Time in nanoseconds after which this thread will terminate if no PRNGs are attached.
-
-
Constructor Detail
-
SimpleRandomSeeder
public SimpleRandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory)
Creates an instance whose thread will terminate if no PRNGs have been associated with it for 5 seconds.- Parameters:
seedGenerator- the seed generatorthreadFactory- theThreadFactorythat will create this seeder's thread
-
SimpleRandomSeeder
public SimpleRandomSeeder(SeedGenerator seedGenerator)
Creates an instance using aSimpleRandomSeeder.DefaultThreadFactory.- Parameters:
seedGenerator- the seed generator
-
SimpleRandomSeeder
public SimpleRandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory, long stopIfEmptyForNanos)
Creates an instance.- Parameters:
seedGenerator- the seed generatorthreadFactory- theThreadFactorythat will create this seeder's threadstopIfEmptyForNanos- time in nanoseconds after which this thread will terminate if no PRNGs are attached
-
-
Method Detail
-
remove
public void remove(Random... randoms)
Removes PRNGs so that they will no longer be reseeded.- Parameters:
randoms- the PRNGs to remove
-
remove
public void remove(Collection<? extends Random> randoms)
Removes PRNGs so that they will no longer be reseeded.- Parameters:
randoms- the PRNGs to remove
-
add
public void add(ByteArrayReseedableRandom... randoms)
AddsByteArrayReseedableRandominstances.- Parameters:
randoms- the PRNGs to start reseeding
-
add
public void add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandominstances.- Parameters:
randoms- the PRNGs to start reseeding
-
wakeUp
public void wakeUp()
Ensures this seeder's thread is started, and signals conditions it may be waiting on.
-
initTransientFields
protected void initTransientFields()
Initializes the transient instance fields for this class. Called by constructors and during deserialization.
-
createSynchronizedWeakHashSet
protected <T> Set<T> createSynchronizedWeakHashSet()
Creates and returns a thread-safeSetbacked by aWeakHashMap.- Type Parameters:
T- the set element type- Returns:
- an empty mutable thread-safe
Setthat holds only weak references to its members
-
iterate
protected boolean iterate()
Description copied from class:LooperThe task that will be iterated until it returns false. Cannot be abstract for serialization reasons, but must be overridden in subclasses if they are instantiated without a targetRunnable.
-
reseedByteArrayReseedableRandoms
protected boolean reseedByteArrayReseedableRandoms()
Reseeds all the PRNGs that need reseeding inbyteArrayPrngsThisIteration, then clears that set.- Returns:
- true if at least one PRNG was reseeded; false otherwise
-
reseedWithLong
protected void reseedWithLong(Random random)
Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long).- Parameters:
random- the PRNG to reseed
-
shutDown
public void shutDown()
Shut down this thread even ifRandominstances are registered with it.
-
clear
protected void clear()
Removes all PRNGs from this seeder.
-
unregisterWithAll
protected void unregisterWithAll(Set<?> randoms)
Informs the given PRNGs that they no longer have a seed generator. Only affects those that supportBaseRandom.setRandomSeeder(SimpleRandomSeeder).- Parameters:
randoms- the PRNGs to unregister with
-
isEmpty
public boolean isEmpty()
Returns true if noRandominstances are registered with this LegacyRandomSeeder.- Returns:
- true if no
Randominstances are registered with this LegacyRandomSeeder.
-
stopIfEmpty
public void stopIfEmpty()
Shut down this thread if noRandominstances are registered with it.
-
getSeedGenerator
public SeedGenerator getSeedGenerator()
Returns theSeedGeneratorthis seeder uses.- Returns:
- this seeder's
SeedGenerator
-
-