diff --git a/src/main/java/com/fasterxml/uuid/impl/LazyRandom.java b/src/main/java/com/fasterxml/uuid/impl/LazyRandom.java index 5d53b0f..5e13e41 100644 --- a/src/main/java/com/fasterxml/uuid/impl/LazyRandom.java +++ b/src/main/java/com/fasterxml/uuid/impl/LazyRandom.java @@ -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) + // 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; + } } } \ No newline at end of file