Get random boolean in Java

后端 未结 10 1555
情深已故
情深已故 2020-12-01 11:28

Okay, I implemented this SO question to my code: Return True or False Randomly

But, I have strange behavior: I need to run ten instances simultaneously, where every

10条回答
  •  -上瘾入骨i
    2020-12-01 12:23

    Java 8: Use random generator isolated to the current thread: ThreadLocalRandom nextBoolean()

    Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention.

    java.util.concurrent.ThreadLocalRandom.current().nextBoolean();
    

提交回复
热议问题