What is the fastest cyclic synchronization in Java (ExecutorService vs. CyclicBarrier vs. X)?

后端 未结 6 898
执笔经年
执笔经年 2021-02-02 15:45

Which Java synchronization construct is likely to provide the best performance for a concurrent, iterative processing scenario with a fixed number of threads like the one outli

6条回答
  •  别跟我提以往
    2021-02-02 16:28

    Update: V5 - Busy Wait in all threads (seems optimal so far)

    Since all cores are dedicated to this task, it seemed worth a try to simply eliminate all the complex synchronization constructs and do a busy wait at each synchronization point in all threads. This turns out to beat all other approaches by a wide margin.

    The setup is as follows: start with V4 above (CyclicBarrier + Busy Wait). Replace the CyclicBarrier with an AtomicInteger that the main thread resets to zero each cycle. Each worker thread Runnable that completes its work increments the atomic integer by one. The main thread busy waits:

    while( true ) {
        // busy-wait for threads to complete their work
        if( atomicInt.get() >= workerThreadCount ) break;
    }
    

    Instead of 8, only 7 worker threads are launched (since all threads, including the main thread, now load a core pretty much completely). The results are as follows:

    blocksize | system | user | cycles/sec
    256k        1.0%     98%       1.36
    64k         1.0%     98%       6.8
    16k         1.0%     98%      44.6
    4096        1.0%     98%     354
    1024        1.0%     98%    1189
    256         1.0%     98%    3222
    64          1.5%     98%    8333
    16          2.0%     98%   16129
    

    Using a wait/notify in the worker threads reduces the throughput to about 1/3rd of this solution.

提交回复
热议问题