Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LazyRandom for native code generation tools #85

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/main/java/com/fasterxml/uuid/impl/LazyRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@
*/
public final class LazyRandom
{
private final static SecureRandom shared = new SecureRandom();
private static final Object lock = new Object();
private static volatile SecureRandom shared;

public static SecureRandom sharedSecureRandom() {
return shared;
// Double check lazy initialization idiom (Effective Java 3rd edition item 11.6)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not called very often, I think I'll modify this to use straight synchronization instead of double-locking.

// Use so that native code generation tools do not detect a SecureRandom instance in a static final field.
SecureRandom result = shared;

if (result != null) {
return result;
}

synchronized (lock) {
result = shared;

if (result == null) {
result = shared = new SecureRandom();
}

return result;
}
}
}