You sleep nanoseconds instead of milliseconds.
I changed with
Thread.sleep(0, 100000 / numberOfThreads); // sleep 0.025 ms for 4 threads
to
Thread.sleep(100000 / numberOfThreads);
and got acceleration proportional to the number of running threads , as expected.
I came up with an intensive processor countPrimes " countPrimes ". Full test code here .
I get the following acceleration on my quad machine:
4 threads: 1625 1 thread: 3747
(The CPU load monitor really shows that in the first case 4 courses are taken, and in the last case 1 core is taken).
Conclusion You do relatively small parts of the work in each thread between synchronization. Synchronization takes much longer than current CPU calculations.
(In addition, if you have intensive code>, for example, array access arrays in threads, the CPU will still not be the bottleneck, and you will not see any acceleration, dividing it into several processors.)
source share