@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 long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } synchronized (ThreadLocalRandom.class) { initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } final long timeoutSeconds = 3; final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { final long waitTime = deadLine - System.nanoTime(); try { final Long seed; if (waitTime <= 0) { seed = seedQueue.poll(); } else { seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS); } if (seed != null) { initialSeedUniquifier = seed; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } if (waitTime <= 0) { seedGeneratorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entropy?", timeoutSeconds ); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); seedGeneratorThread.interrupt(); } if (seedGeneratorEndTime == 0) { seedGeneratorEndTime = System.nanoTime(); } return initialSeedUniquifier; } }
ThreadLocalRandom extends Random { public static long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } synchronized (ThreadLocalRandom.class) { initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } final long timeoutSeconds = 3; final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { final long waitTime = deadLine - System.nanoTime(); try { final Long seed; if (waitTime <= 0) { seed = seedQueue.poll(); } else { seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS); } if (seed != null) { initialSeedUniquifier = seed; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } if (waitTime <= 0) { seedGeneratorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entropy?", timeoutSeconds ); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); seedGeneratorThread.interrupt(); } if (seedGeneratorEndTime == 0) { seedGeneratorEndTime = System.nanoTime(); } return initialSeedUniquifier; } } }
ThreadLocalRandom extends Random { public static long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } synchronized (ThreadLocalRandom.class) { initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } final long timeoutSeconds = 3; final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { final long waitTime = deadLine - System.nanoTime(); try { final Long seed; if (waitTime <= 0) { seed = seedQueue.poll(); } else { seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS); } if (seed != null) { initialSeedUniquifier = seed; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } if (waitTime <= 0) { seedGeneratorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entropy?", timeoutSeconds ); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); seedGeneratorThread.interrupt(); } if (seedGeneratorEndTime == 0) { seedGeneratorEndTime = System.nanoTime(); } return initialSeedUniquifier; } } ThreadLocalRandom(); }
ThreadLocalRandom extends Random { public static long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } synchronized (ThreadLocalRandom.class) { initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } final long timeoutSeconds = 3; final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { final long waitTime = deadLine - System.nanoTime(); try { final Long seed; if (waitTime <= 0) { seed = seedQueue.poll(); } else { seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS); } if (seed != null) { initialSeedUniquifier = seed; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } if (waitTime <= 0) { seedGeneratorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entropy?", timeoutSeconds ); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); seedGeneratorThread.interrupt(); } if (seedGeneratorEndTime == 0) { seedGeneratorEndTime = System.nanoTime(); } return initialSeedUniquifier; } } ThreadLocalRandom(); static void setInitialSeedUniquifier(long initialSeedUniquifier); static 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 long getInitialSeedUniquifier() { long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } synchronized (ThreadLocalRandom.class) { initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier; if (initialSeedUniquifier != 0) { return initialSeedUniquifier; } final long timeoutSeconds = 3; final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds); boolean interrupted = false; for (;;) { final long waitTime = deadLine - System.nanoTime(); try { final Long seed; if (waitTime <= 0) { seed = seedQueue.poll(); } else { seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS); } if (seed != null) { initialSeedUniquifier = seed; break; } } catch (InterruptedException e) { interrupted = true; logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException."); break; } if (waitTime <= 0) { seedGeneratorThread.interrupt(); logger.warn( "Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entropy?", timeoutSeconds ); break; } } initialSeedUniquifier ^= 0x3255ecdc33bae119L; initialSeedUniquifier ^= Long.reverse(System.nanoTime()); ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier; if (interrupted) { Thread.currentThread().interrupt(); seedGeneratorThread.interrupt(); } if (seedGeneratorEndTime == 0) { seedGeneratorEndTime = System.nanoTime(); } return initialSeedUniquifier; } } ThreadLocalRandom(); static void setInitialSeedUniquifier(long initialSeedUniquifier); static 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);  }