Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Prevent loading ThreadLocalRandom in Java 6 environments (#138)
Browse files Browse the repository at this point in the history
I'm not sure why but the JVM seems to load all classes which
are used in a (static) method. That makes it not safe to just
check the threadLocalRandomPresent flag and access
ThreadLocalRandom right away.
  • Loading branch information
felixbarny authored and yurishkuro committed Apr 19, 2017
1 parent e340486 commit b06e541
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,24 @@ protected Random initialValue() {
*
* @return the current thread's {@link Random}
*/
@IgnoreJRERequirement
public static Random current() {
if (threadLocalRandomPresent) {
return ThreadLocalRandom.current();
return ThreadLocalRandomAccessor.getCurrentThreadLocalRandom();
} else {
return threadLocal.get();
}
}

/**
* This class prevents that {@link ThreadLocalRandom} gets loaded unless
* {@link #getCurrentThreadLocalRandom()} is called
*/
private static class ThreadLocalRandomAccessor {
@IgnoreJRERequirement
private static Random getCurrentThreadLocalRandom() {
return ThreadLocalRandom.current();
}
}

private Java6CompatibleThreadLocalRandom() {}
}

0 comments on commit b06e541

Please sign in to comment.