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?
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
.