Is Random class thread safe?

前端 未结 8 955
臣服心动
臣服心动 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:05

    The Random class is not set up for one instance to be used in multiple threads. Ofcourse, if you did this, likely you will increase the possibility of getting un-predictable and closer to random numbers. But since it is a pseudo-random generator, I cannot see why you would need to share an instance. Is there a more specific requirement?

提交回复
热议问题