Java - multithreaded code does not run faster on more cores

后端 未结 5 1645
半阙折子戏
半阙折子戏 2021-02-10 22:59

I was just running some multithreaded code on a 4-core machine in the hopes that it would be faster than on a single-core machine. Here\'s the idea: I got a fixed number of thre

5条回答
  •  日久生厌
    2021-02-10 23:14

    You're sleeping nano-seconds instead of milli-seconds.

    I changed from

    Thread.sleep(0, 100000 / numberOfThreads); // sleep 0.025 ms for 4 threads
    

    to

    Thread.sleep(100000 / numberOfThreads);
    

    and got a speed-up proportional to the number of threads started just as expected.


    I invented a CPU-intensive "countPrimes". Full test code available here.

    I get the following speed-up on my quad-core machine:

    4 threads: 1625
    1 thread: 3747
    

    (the CPU-load monitor indeed shows that 4 course are busy in the former case, and that 1 core is busy in the latter case.)

    Conclusion: You're doing comparatively small portions of work in each thread between synchronization. The synchronization takes much much more time than the actual CPU-intensive computation work.

    (Also, if you have memory intensive code, such as tons of array-accesses in the threads, the CPU won't be the bottle-neck anyway, and you won't see any speed-up by splitting it on multiple CPUs.)

提交回复
热议问题