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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…