The clue is that you are calling Math.random
which uses a single global instance of Random
. So, all your 4 threads compete for the one resource.
Using a thread local Random
object will make your execution really parallel:
Random random = new Random();
double[] array = new double[10000000];
for (int i = 0; i < array.length; i++) {
array[i] = Math.tanh(random.nextDouble());
}