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 overByteArrayReseedableRandom
instances and reseeds them. NoEntropyCountingRandom
will 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 class
SimpleRandomSeeder.DefaultThreadFactory
AThreadFactory
that sets the name and priority of the threads it creates.
-
Field Summary
Fields Modifier and Type Field Description protected Set<ByteArrayReseedableRandom>
byteArrayPrngs
HoldsByteArrayReseedableRandom
instances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom
.protected 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 theConcurrentModificationException
that would otherwise arise.protected static long
DEFAULT_STOP_IF_EMPTY_FOR_NANOS
Default waiting time before an empty instance terminates if still empty.protected static long
POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy.protected SeedGenerator
seedGenerator
The seed generator this seeder uses.protected long
stopIfEmptyForNanos
Time in nanoseconds after which this thread will terminate if no PRNGs are attached.protected Condition
waitForEntropyDrain
Signaled by an associatedBaseRandom
when it runs out of entropy.protected Condition
waitWhileEmpty
Signaled 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 void
add(ByteArrayReseedableRandom... randoms)
AddsByteArrayReseedableRandom
instances.void
add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandom
instances.protected void
clear()
Removes all PRNGs from this seeder.protected <T> Set<T>
createSynchronizedWeakHashSet()
Creates and returns a thread-safeSet
backed by aWeakHashMap
.boolean
equals(Object o)
SeedGenerator
getSeedGenerator()
Returns theSeedGenerator
this seeder uses.int
hashCode()
protected void
initTransientFields()
Initializes the transient instance fields for this class.boolean
isEmpty()
Returns true if noRandom
instances are registered with this LegacyRandomSeeder.protected boolean
iterate()
The task that will be iterated until it returns false.void
remove(Collection<? extends Random> randoms)
Removes PRNGs so that they will no longer be reseeded.void
remove(Random... randoms)
Removes PRNGs so that they will no longer be reseeded.protected boolean
reseedByteArrayReseedableRandoms()
Reseeds all the PRNGs that need reseeding inbyteArrayPrngsThisIteration
, then clears that set.protected void
reseedWithLong(Random random)
Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long)
.void
shutDown()
Shut down this thread even ifRandom
instances are registered with it.void
stopIfEmpty()
Shut down this thread if noRandom
instances are registered with it.protected void
unregisterWithAll(Set<?> randoms)
Informs the given PRNGs that they no longer have a seed generator.void
wakeUp()
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
HoldsByteArrayReseedableRandom
instances 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 theConcurrentModificationException
that would otherwise arise.
-
waitWhileEmpty
protected transient Condition waitWhileEmpty
Signaled when a PRNG is added.
-
waitForEntropyDrain
protected transient Condition waitForEntropyDrain
Signaled by an associatedBaseRandom
when 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
- theThreadFactory
that 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
- theThreadFactory
that 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)
AddsByteArrayReseedableRandom
instances.- Parameters:
randoms
- the PRNGs to start reseeding
-
add
public void add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandom
instances.- 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-safeSet
backed by aWeakHashMap
.- Type Parameters:
T
- the set element type- Returns:
- an empty mutable thread-safe
Set
that holds only weak references to its members
-
iterate
protected boolean iterate()
Description copied from class:Looper
The 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 ifRandom
instances 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 noRandom
instances are registered with this LegacyRandomSeeder.- Returns:
- true if no
Random
instances are registered with this LegacyRandomSeeder.
-
stopIfEmpty
public void stopIfEmpty()
Shut down this thread if noRandom
instances are registered with it.
-
getSeedGenerator
public SeedGenerator getSeedGenerator()
Returns theSeedGenerator
this seeder uses.- Returns:
- this seeder's
SeedGenerator
-
-