113

Is SecureRandom thread safe? That is, after initializing it, can access to the next random number be relied on to be thread safe? Examining the source code seems to show that it is, and this bug report seems to indicate that its lack of documentation as thread safe is a javadoc issue. Has anyone confirmed that it is in fact thread safe?

3 Answers 3

119

Yes, it is. It extends Random, which always had a de facto threadsafe implementation, and, from Java 7, explicitly guarantees threadsafety.

If many threads are using a single SecureRandom, there might be contention that hurts performance. On the other hand, initializing a SecureRandom instance can be relatively slow. Whether it is best to share a global RNG, or to create a new one for each thread will depend on your application. The ThreadLocalRandom class could be used as a pattern to provide a solution that supports SecureRandom.

14
  • 3
    Thanks for the update. Oddly, the bug is marked closed as "will not fix." But they fixed it anyway. Oh well, I don't envy them the size of their bug database.
    – Yishai
    Sep 14, 2011 at 19:30
  • 4
    initializing a SecureRandom can not only be slow, but can potentially hang because of missing entropy Jul 25, 2014 at 9:37
  • 10
    Please keep in mind that ThreadLocalRandom is very easy to crack, so if you plan to expose generated value to the world, use SecureRandom instead jazzy.id.au/default/2010/09/20/…
    – walv
    Aug 4, 2014 at 9:19
  • 4
    I'm going to go out on a limb here and say this answer is incorrect. The contract for Random, which guarantees thread safety, is not binding on subclasses. Certainly all the other properties of Random documented are not binding on subclasses, so I don't see why thread-safety should be assumed. Dec 6, 2017 at 18:16
  • 2
    @JamesKPolk Failure to preserve a property of the supertype would violate the substitutability principle.
    – erickson
    Jan 31, 2018 at 7:14
13

The current implementation of SecureRandom is thread safe, specifically the two mutating methods nextBytes(bytes[]) and setSeed(byte[]) are synchronized.

Well, as far as I've been able to tell, all mutating methods are eventually routed through those two methods, and SecureRandom overrides a few methods in Random to ensure that. Which works but could be brittle if the implementation is changed in the future.

The best solution is to manually synchronize on the SecureRandom instance first. This means each call stack will acquire two locks on the same object, but that is usually very cheap on modern JVMs. That is, there is not much harm in explicitly synchronizing yourself. For example:

    SecureRandom rnd = ...;

    byte[] b = new byte[NRANDOM_BYTES];
    synchronized (rnd) {
        rnd.nextBytes(b);
    }
2
  • 3
    At least in JDK 10, SecureRandom is based on a provider and checks if the provider is thread safe, only synchronizing if it's not, in nextBytes.
    – nafg
    Feb 6, 2019 at 18:10
  • 1
    java.security.SecureRandom#nextBytes in Java 8 is not synchronized. Could you please specify in what Java version did you find a synchronized #nextBytes?. Mar 13, 2019 at 1:54
3

Please see https://bugs.openjdk.java.net/browse/JDK-8165115 which was fixed in Java 9.

It says:

SecureRandom objects are safe for use by multiple concurrent threads. A SecureRandom service provider can advertise that it is thread-safe by setting the service provider attribute "ThreadSafe" to "true" when registering the provider. Otherwise, the SecureRandom class will synchronize access to the following SecureRandomSpi methods: SecureRandomSpi.engineSetSeed(byte[]), SecureRandomSpi.engineNextBytes(byte[]), SecureRandomSpi.engineNextBytes(byte[], SecureRandomParameters), SecureRandomSpi.engineGenerateSeed(int), and SecureRandomSpi.engineReseed(SecureRandomParameters).

1
  • Thank you for the much needed >10 year update! Dec 30, 2021 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.