Is Random class thread safe?

前端 未结 8 963
臣服心动
臣服心动 2020-12-13 11:53

Is it valid to share one instance of the Random class between multiple threads? And to call nextInt(int) from multiple threads in particular?

8条回答
  •  青春惊慌失措
    2020-12-13 12:19

    Acording to the documentation, Math.random() guarantees it's safe for use by multiple threads. But the Random class does not. I would assume then you'll have to synchronize that yourself.

提交回复
热议问题