Class SimpleRandomSeeder

    • 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.
      • 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 the ConcurrentModificationException that would otherwise arise.
      • waitWhileEmpty

        protected transient Condition waitWhileEmpty
        Signaled when a PRNG is added.
      • waitForEntropyDrain

        protected transient Condition waitForEntropyDrain
        Signaled by an associated BaseRandom 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 generator
        threadFactory - the ThreadFactory that will create this seeder's thread
      • SimpleRandomSeeder

        public SimpleRandomSeeder​(SeedGenerator seedGenerator,
                                  ThreadFactory threadFactory,
                                  long stopIfEmptyForNanos)
        Creates an instance.
        Parameters:
        seedGenerator - the seed generator
        threadFactory - the ThreadFactory that will create this seeder's thread
        stopIfEmptyForNanos - 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
      • wakeUp

        public void wakeUp()
        Ensures this seeder's thread is started, and signals conditions it may be waiting on.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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-safe Set backed by a WeakHashMap.
        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 target Runnable.
        Specified by:
        iterate in class Looper
        Returns:
        true if this thread should iterate again.
      • reseedByteArrayReseedableRandoms

        protected boolean reseedByteArrayReseedableRandoms()
        Reseeds all the PRNGs that need reseeding in byteArrayPrngsThisIteration, 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 calls Random.setSeed(long).
        Parameters:
        random - the PRNG to reseed
      • shutDown

        public void shutDown()
        Shut down this thread even if Random 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 support BaseRandom.setRandomSeeder(SimpleRandomSeeder).
        Parameters:
        randoms - the PRNGs to unregister with
      • isEmpty

        public boolean isEmpty()
        Returns true if no Random 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 no Random instances are registered with it.