@Test public void getInitialSeedUniquifierPreservesInterrupt() { try { Thread.currentThread().interrupt(); assertTrue("Assert that thread is interrupted before invocation of getInitialSeedUniquifier()", Thread.currentThread().isInterrupted()); ThreadLocalRandom.getInitialSeedUniquifier(); assertTrue("Assert that thread is interrupted after invocation of getInitialSeedUniquifier()", Thread.currentThread().isInterrupted()); } finally { Thread.interrupted(); } }
public static synchronized long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier == 0) { ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0); } if (initialSeedUniquifier == 0) { final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>(); Thread generatorThread = new Thread("initialSeedUniquifierGenerator") { @Override public void run() { SecureRandom random = new SecureRandom(); queue.add(random.generateSeed(8)); } }; generatorThread.setDaemon(true); generatorThread.start(); generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { logger.debug("An exception has been raised by {}", t.getName(), e); } }); final long timeoutSeconds = 3; final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { long waitTime = deadLine - System.nanoTime(); if (waitTime <= 0) { generatorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds ); break; } try { byte[] seed = queue.poll(waitTime, TimeUnit.NANOSECONDS); if (seed != null) { initialSeedUniquifier = ((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; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); generatorThread.interrupt(); } } return initialSeedUniquifier; }
ThreadLocalRandom extends Random { public static synchronized long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier == 0) { ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0); } if (initialSeedUniquifier == 0) { final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>(); Thread generatorThread = new Thread("initialSeedUniquifierGenerator") { @Override public void run() { SecureRandom random = new SecureRandom(); queue.add(random.generateSeed(8)); } }; generatorThread.setDaemon(true); generatorThread.start(); generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { logger.debug("An exception has been raised by {}", t.getName(), e); } }); final long timeoutSeconds = 3; final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { long waitTime = deadLine - System.nanoTime(); if (waitTime <= 0) { generatorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds ); break; } try { byte[] seed = queue.poll(waitTime, TimeUnit.NANOSECONDS); if (seed != null) { initialSeedUniquifier = ((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; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); generatorThread.interrupt(); } } return initialSeedUniquifier; } }
ThreadLocalRandom extends Random { public static synchronized long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier == 0) { ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0); } if (initialSeedUniquifier == 0) { final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>(); Thread generatorThread = new Thread("initialSeedUniquifierGenerator") { @Override public void run() { SecureRandom random = new SecureRandom(); queue.add(random.generateSeed(8)); } }; generatorThread.setDaemon(true); generatorThread.start(); generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { logger.debug("An exception has been raised by {}", t.getName(), e); } }); final long timeoutSeconds = 3; final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { long waitTime = deadLine - System.nanoTime(); if (waitTime <= 0) { generatorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds ); break; } try { byte[] seed = queue.poll(waitTime, TimeUnit.NANOSECONDS); if (seed != null) { initialSeedUniquifier = ((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; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); generatorThread.interrupt(); } } return initialSeedUniquifier; } ThreadLocalRandom(); }
ThreadLocalRandom extends Random { public static synchronized long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier == 0) { ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0); } if (initialSeedUniquifier == 0) { final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>(); Thread generatorThread = new Thread("initialSeedUniquifierGenerator") { @Override public void run() { SecureRandom random = new SecureRandom(); queue.add(random.generateSeed(8)); } }; generatorThread.setDaemon(true); generatorThread.start(); generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { logger.debug("An exception has been raised by {}", t.getName(), e); } }); final long timeoutSeconds = 3; final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { long waitTime = deadLine - System.nanoTime(); if (waitTime <= 0) { generatorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds ); break; } try { byte[] seed = queue.poll(waitTime, TimeUnit.NANOSECONDS); if (seed != null) { initialSeedUniquifier = ((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; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); generatorThread.interrupt(); } } return initialSeedUniquifier; } ThreadLocalRandom(); static void setInitialSeedUniquifier(long initialSeedUniquifier); static synchronized long getInitialSeedUniquifier(); static ThreadLocalRandom current(); void setSeed(long seed); int nextInt(int least, int bound); long nextLong(long n); long nextLong(long least, long bound); double nextDouble(double n); double nextDouble(double least, double bound); }
ThreadLocalRandom extends Random { public static synchronized long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier == 0) { ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0); } if (initialSeedUniquifier == 0) { final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>(); Thread generatorThread = new Thread("initialSeedUniquifierGenerator") { @Override public void run() { SecureRandom random = new SecureRandom(); queue.add(random.generateSeed(8)); } }; generatorThread.setDaemon(true); generatorThread.start(); generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { logger.debug("An exception has been raised by {}", t.getName(), e); } }); final long timeoutSeconds = 3; final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { long waitTime = deadLine - System.nanoTime(); if (waitTime <= 0) { generatorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds ); break; } try { byte[] seed = queue.poll(waitTime, TimeUnit.NANOSECONDS); if (seed != null) { initialSeedUniquifier = ((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; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); generatorThread.interrupt(); } } return initialSeedUniquifier; } ThreadLocalRandom(); static void setInitialSeedUniquifier(long initialSeedUniquifier); static synchronized long getInitialSeedUniquifier(); static ThreadLocalRandom current(); void setSeed(long seed); int nextInt(int least, int bound); long nextLong(long n); long nextLong(long least, long bound); double nextDouble(double n); double nextDouble(double least, double bound);  }