Class RandomSeeder

    • Field Detail

      • FIRST_POLL_INTERVAL

        protected static final long FIRST_POLL_INTERVAL
        Time in seconds to wait before checking again whether any PRNGs need more entropy, after one iteration when they didn't.
        See Also:
        Constant Field Values
      • REPEAT_POLL_INTERVAL

        protected static final long REPEAT_POLL_INTERVAL
        Time in seconds to wait before checking again whether any PRNGs need more entropy, after more than one iteration when they didn't.
        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.
      • 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

      • RandomSeeder

        public RandomSeeder​(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
      • RandomSeeder

        public RandomSeeder​(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​(ByteArrayReseedableRandom... randoms)
        Removes PRNGs so that they will no longer be reseeded.
        Parameters:
        randoms - the PRNGs to remove
      • remove

        public void remove​(Collection<? extends ByteArrayReseedableRandom> randoms)
        Removes PRNGs so that they will no longer be reseeded.
        Parameters:
        randoms - the PRNGs to remove
      • contains

        public boolean contains​(Object random)
        Checks whether the given PRNG is currently registered with this RandomSeeder.
        Parameters:
        random - the PRNG to check the status of
        Returns:
        true if registered; false if not
      • 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.
        Overrides:
        initTransientFields in class Looper
      • 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.
      • waitForEntropyDrainOrUpdateFlag

        protected void waitForEntropyDrainOrUpdateFlag​(boolean entropyConsumed)
                                                throws InterruptedException
        If entropy was consumed this iteration, waits until wakeUp() is called or the polling interval expires. Updates whether the next polling wait will be short or long.
        Parameters:
        entropyConsumed - whether entropy was consumed this iteration
        Throws:
        InterruptedException - if interrupted
      • reseedByteArrayReseedableRandoms

        protected boolean reseedByteArrayReseedableRandoms​(Iterable<? extends ByteArrayReseedableRandom> randoms)
        Reseeds all the PRNGs that need reseeding in randoms.
        Parameters:
        randoms - the PRNGs to reseed
        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 static void unregisterWithAll​(Set<?> randoms)
        Informs the given PRNGs that they no longer have a seed generator. Only affects those that support BaseRandom.setRandomSeeder(RandomSeeder).
        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.