thread local random
package com.baeldung.concurrent.blockingqueue;
-import java.util.Random;
import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ThreadLocalRandom;
class NumbersProducer implements Runnable {
- private final Random random = new Random();
private final BlockingQueue<Integer> numbersQueue;
private final int poisonPill;
private final int poisonPillPerProducer;
@@ -29,7 +28,7 @@ class NumbersProducer implements Runnable {
private void generateNumbers() throws InterruptedException {
for (int i = 0; i < 100; i++) {
- numbersQueue.put(random.nextInt(100));
+ numbersQueue.put(ThreadLocalRandom.current().nextInt(100));
}
for (int j = 0; j < poisonPillPerProducer; j++) {
numbersQueue.put(poisonPill);
