Why is Arrays.setAll faster than Arrays.parallelSetAll in this case?

Viewed 546

Why is Arrays.setAll faster than Arrays.parallelSetAll in this case?

int[] array = new int[30000000];
Random rnd = new Random();
Arrays.parallelSetAll(array, i->rnd.nextInt(200));
1 Answers

From the javadoc:

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Related