Home / Class/ ThreadLocalRandom Class — netty Architecture

ThreadLocalRandom Class — netty Architecture

Architecture documentation for the ThreadLocalRandom class in ThreadLocalRandom.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  cad32658_3724_a99b_a2c9_78d6688e22b6["ThreadLocalRandom"]
  b2d044e9_37c8_9e93_c3a1_35bb0ee42c82["ThreadLocalRandom.java"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|defined in| b2d044e9_37c8_9e93_c3a1_35bb0ee42c82
  6222884d_473f_9e7c_d9b3_85e799e3a534["setInitialSeedUniquifier()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| 6222884d_473f_9e7c_d9b3_85e799e3a534
  934f1d75_fad9_3323_2acb_33ba4aa751de["getInitialSeedUniquifier()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| 934f1d75_fad9_3323_2acb_33ba4aa751de
  6aab6f7b_9b36_71ef_c98a_e545b4b09e28["newSeed()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| 6aab6f7b_9b36_71ef_c98a_e545b4b09e28
  efad75ac_7fa6_954c_2061_59407a23884a["mix64()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| efad75ac_7fa6_954c_2061_59407a23884a
  fade03da_c357_2412_6ab9_f5612ffce139["ThreadLocalRandom()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| fade03da_c357_2412_6ab9_f5612ffce139
  cbbeb62d_cd6a_1f09_d15a_00ab3896b102["setSeed()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| cbbeb62d_cd6a_1f09_d15a_00ab3896b102
  bbbaa728_e140_8728_d47e_e3846f949988["next()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| bbbaa728_e140_8728_d47e_e3846f949988
  50844849_8964_e3f4_f90b_e3092753567c["nextInt()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| 50844849_8964_e3f4_f90b_e3092753567c
  b776a5b2_2836_d36a_601c_20ffeadcb95d["nextLong()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| b776a5b2_2836_d36a_601c_20ffeadcb95d
  1d2f609a_5383_4001_6421_657714394122["nextDouble()"]
  cad32658_3724_a99b_a2c9_78d6688e22b6 -->|method| 1d2f609a_5383_4001_6421_657714394122

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/ThreadLocalRandom.java lines 62–387

@Deprecated
@SuppressWarnings("all")
public final class ThreadLocalRandom extends Random {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(ThreadLocalRandom.class);

    private static final AtomicLong seedUniquifier = new AtomicLong();

    private static volatile long initialSeedUniquifier;

    private static final Thread seedGeneratorThread;
    private static final BlockingQueue<Long> seedQueue;
    private static final long seedGeneratorStartTime;
    private static volatile long seedGeneratorEndTime;

    static {
        initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0);
        if (initialSeedUniquifier == 0) {
            boolean secureRandom = SystemPropertyUtil.getBoolean("java.util.secureRandomSeed", false);
            if (secureRandom) {
                seedQueue = new LinkedBlockingQueue<Long>();
                seedGeneratorStartTime = System.nanoTime();

                // Try to generate a real random number from /dev/random.
                // Get from a different thread to avoid blocking indefinitely on a machine without much entropy.
                seedGeneratorThread = new Thread("initialSeedUniquifierGenerator") {
                    @Override
                    public void run() {
                        final SecureRandom random = new SecureRandom(); // Get the real random seed from /dev/random
                        final byte[] seed = random.generateSeed(8);
                        seedGeneratorEndTime = System.nanoTime();
                        long s = ((long) seed[0] & 0xff) << 56 |
                                 ((long) seed[1] & 0xff) << 48 |
                                 ((long) seed[2] & 0xff) << 40 |
                                 ((long) seed[3] & 0xff) << 32 |
                                 ((long) seed[4] & 0xff) << 24 |
                                 ((long) seed[5] & 0xff) << 16 |
                                 ((long) seed[6] & 0xff) <<  8 |
                                 (long) seed[7] & 0xff;
                        seedQueue.add(s);
                    }
                };
                seedGeneratorThread.setDaemon(true);
                seedGeneratorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        logger.debug("An exception has been raised by {}", t.getName(), e);
                    }
                });
                seedGeneratorThread.start();
            } else {
                initialSeedUniquifier = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());
                seedGeneratorThread = null;
                seedQueue = null;
                seedGeneratorStartTime = 0L;
            }
        } else {
            seedGeneratorThread = null;
            seedQueue = null;
            seedGeneratorStartTime = 0L;
        }
    }

    public static void setInitialSeedUniquifier(long initialSeedUniquifier) {
        ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
    }

    public static long getInitialSeedUniquifier() {
        // Use the value set via the setter.
        long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
        if (initialSeedUniquifier != 0) {
            return initialSeedUniquifier;
        }

        synchronized (ThreadLocalRandom.class) {
            initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
            if (initialSeedUniquifier != 0) {
                return initialSeedUniquifier;
            }

            // Get the random seed from the generator thread with timeout.

Frequently Asked Questions

What is the ThreadLocalRandom class?
ThreadLocalRandom is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/ThreadLocalRandom.java.
Where is ThreadLocalRandom defined?
ThreadLocalRandom is defined in common/src/main/java/io/netty/util/internal/ThreadLocalRandom.java at line 62.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free