Changed to use ThreadLocalRandom instead of Random
package com.baeldung.concurrent.blockingqueue;
-import java.util.Random;
import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ThreadLocalRandom;
public class NumbersProducer implements Runnable {
- private final Random random = new Random();
+
private final BlockingQueue<Integer> numbersQueue;
public NumbersProducer(BlockingQueue<Integer> numbersQueue) {
@@ -21,7 +21,7 @@ public 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));
}
}
}
\ No newline at end of file
