Java6CompatibleThreadLocalRandom { public static Random current() { if (threadLocalRandomPresent) { return ThreadLocalRandomAccessor.getCurrentThreadLocalRandom(); } else { return threadLocal.get(); } } private  Java6CompatibleThreadLocalRandom(); static Random current();  }
@Test public void testThreadLocalRandomPresent() throws Exception { assertSame(Java6CompatibleThreadLocalRandom.current(), ThreadLocalRandom.current()); assertNotNull(Java6CompatibleThreadLocalRandom.current().nextLong()); }
@Test public void testThreadLocalRandomNotPresent() throws Exception { Java6CompatibleThreadLocalRandom.threadLocalRandomPresent = false; assertNotSame(Java6CompatibleThreadLocalRandom.current(), ThreadLocalRandom.current()); assertNotNull(Java6CompatibleThreadLocalRandom.current().nextLong()); }
@Test public void testRandomFromOtherThreadIsNotSame() throws Exception { Java6CompatibleThreadLocalRandom.threadLocalRandomPresent = false; final AtomicReference<Random> randomFromOtherThread = new AtomicReference<>(); final Thread thread = new Thread(new Runnable() { @Override public void run() { randomFromOtherThread.set(Java6CompatibleThreadLocalRandom.current()); } }); thread.start(); thread.join(); assertNotSame(Java6CompatibleThreadLocalRandom.current(), randomFromOtherThread.get()); assertNotNull(Java6CompatibleThreadLocalRandom.current().nextLong()); assertNotNull(randomFromOtherThread.get()); assertNotNull(randomFromOtherThread.get().nextLong()); }