Skip to content
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;
}
}
}