< prev index next >

test/jdk/java/util/concurrent/tck/ThreadLocalRandomTest.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


 367                 while (i < NCALLS &&
 368                        (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
 369                     assertTrue(least <= j && j < bound);
 370                     ++i;
 371                 }
 372                 assertTrue(i < NCALLS);
 373             }
 374         }
 375     }
 376 
 377     /**
 378      * Different threads produce different pseudo-random sequences
 379      */
 380     public void testDifferentSequences() {
 381         // Don't use main thread's ThreadLocalRandom - it is likely to
 382         // be polluted by previous tests.
 383         final AtomicReference<ThreadLocalRandom> threadLocalRandom =
 384             new AtomicReference<>();
 385         final AtomicLong rand = new AtomicLong();
 386 
 387         long firstRand = 0;
 388         ThreadLocalRandom firstThreadLocalRandom = null;
 389 
 390         Runnable getRandomState = new CheckedRunnable() {
 391             public void realRun() {
 392                 ThreadLocalRandom current = ThreadLocalRandom.current();
 393                 assertSame(current, ThreadLocalRandom.current());
 394                 // test bug: the following is not guaranteed and not true in JDK8
 395                 //                assertNotSame(current, threadLocalRandom.get());
 396                 rand.set(current.nextLong());
 397                 threadLocalRandom.set(current);
 398             }};
 399 
 400         Thread first = newStartedThread(getRandomState);
 401         awaitTermination(first);
 402         firstRand = rand.get();
 403         firstThreadLocalRandom = threadLocalRandom.get();
 404 
 405         for (int i = 0; i < NCALLS; i++) {
 406             Thread t = newStartedThread(getRandomState);
 407             awaitTermination(t);


 408             if (firstRand != rand.get())
 409                 return;
 410         }
 411         fail("all threads generate the same pseudo-random sequence");
 412     }
 413 
 414     /**
 415      * Repeated calls to nextBytes produce at least values of different signs for every byte
 416      */
 417     public void testNextBytes() {
 418         ThreadLocalRandom rnd = ThreadLocalRandom.current();
 419         int n = rnd.nextInt(1, 20);
 420         byte[] bytes = new byte[n];
 421         outer:
 422         for (int i = 0; i < n; i++) {
 423             for (int tries = NCALLS; tries-->0; ) {
 424                 byte before = bytes[i];
 425                 rnd.nextBytes(bytes);
 426                 byte after = bytes[i];
 427                 if (after * before < 0)




 367                 while (i < NCALLS &&
 368                        (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
 369                     assertTrue(least <= j && j < bound);
 370                     ++i;
 371                 }
 372                 assertTrue(i < NCALLS);
 373             }
 374         }
 375     }
 376 
 377     /**
 378      * Different threads produce different pseudo-random sequences
 379      */
 380     public void testDifferentSequences() {
 381         // Don't use main thread's ThreadLocalRandom - it is likely to
 382         // be polluted by previous tests.
 383         final AtomicReference<ThreadLocalRandom> threadLocalRandom =
 384             new AtomicReference<>();
 385         final AtomicLong rand = new AtomicLong();
 386 



 387         Runnable getRandomState = new CheckedRunnable() {
 388             public void realRun() {
 389                 ThreadLocalRandom current = ThreadLocalRandom.current();
 390                 assertSame(current, ThreadLocalRandom.current());


 391                 rand.set(current.nextLong());
 392                 threadLocalRandom.set(current);
 393             }};
 394 
 395         awaitTermination(newStartedThread(getRandomState));
 396         long firstRand = rand.get();
 397         ThreadLocalRandom firstThreadLocalRandom = threadLocalRandom.get();
 398         assertNotNull(firstThreadLocalRandom);
 399 
 400         for (int i = 0; i < NCALLS; i++) {
 401             awaitTermination(newStartedThread(getRandomState));
 402             if (testImplementationDetails)
 403                 // ThreadLocalRandom has been a singleton since jdk8.
 404                 assertSame(firstThreadLocalRandom, threadLocalRandom.get());
 405             if (firstRand != rand.get())
 406                 return;
 407         }
 408         fail("all threads generate the same pseudo-random sequence");
 409     }
 410 
 411     /**
 412      * Repeated calls to nextBytes produce at least values of different signs for every byte
 413      */
 414     public void testNextBytes() {
 415         ThreadLocalRandom rnd = ThreadLocalRandom.current();
 416         int n = rnd.nextInt(1, 20);
 417         byte[] bytes = new byte[n];
 418         outer:
 419         for (int i = 0; i < n; i++) {
 420             for (int tries = NCALLS; tries-->0; ) {
 421                 byte before = bytes[i];
 422                 rnd.nextBytes(bytes);
 423                 byte after = bytes[i];
 424                 if (after * before < 0)


< prev index next >