Random over ThreadLocalRandom

后端 未结 6 1851
梦谈多话
梦谈多话 2020-12-02 22:48

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention an

6条回答
  •  独厮守ぢ
    2020-12-02 23:05

    A Random instance can only provide a random number to one thread at a time. So, if you have many threads simultaneously requesting random numbers from that instance, that tends to slow down all the threads.

    On the other hand, each thread would have its own ThreadLocalRandom instance, so no threads would be blocked when requesting a random number.

提交回复
热议问题