更新时间:2023-01-11 21:32:08
是的.它扩展了Random
,它总是具有事实线程安全的实现,并且从
Yes, it is. It extends Random
, which always had a de facto threadsafe implementation, and, from Java 7, explicitly guarantees threadsafety.
如果许多线程使用单个SecureRandom
,则可能存在争用,从而影响性能.另一方面,初始化SecureRandom
实例可能相对较慢.共享全局RNG还是为每个线程创建一个新的RNG取决于您的应用程序. ThreadLocalRandom
类可能是用作模式以提供支持SecureRandom
的解决方案.
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
.