OpenHFT / Java-Thread-Affinity

Bind a java thread to a given core
http://chronicle.software/products/thread-affinity/
Apache License 2.0
1.78k stars 360 forks source link

In macOS, AffinityThreadFactory is slower than the default thread factory #98

Closed TDtianzhenjiu closed 1 year ago

TDtianzhenjiu commented 1 year ago

Hello @hft-team-city As the title described, I tested AffinityThreadFactory on my macOS, and the result with AffinityThreadFactory was consistently slower than the default threadfactory, the code below

val threadFactory = AffinityThreadFactory("affinity",AffinityStrategies.DIFFERENT_CORE)
    val threadPool= Executors.newFixedThreadPool(8,threadFactory)
    val futures: ArrayList<Future<Long>> = ArrayList()
    val runnable = runable@{

        val l1=System.nanoTime()
        for (j in 1..5000){
            var sum = 0
            for (i in 1..10000){
                sum +=i
            }
        }
        val l2=System.nanoTime()
        return@runable l2-l1;
    }

    var l1=System.currentTimeMillis()
    for (i in 1..8){
       futures.add( threadPool.submit(runnable))
    }

    for (i in futures){
       i.get()
    }
    var l2=System.currentTimeMillis();

    println("-----------------"+(l2-l1))
    threadPool.shutdownNow()

When I use affinity thread factory, it costs about 1400ms~1700ms however, I remove the thread factory as below: val threadPool= Executors.newFixedThreadPool(8)

It costs about 700ms~1100ms

I know the affinity factory will bind a thread to the CPU core, but why is it slower than a thread that never bound the CPU core?

JerryShea commented 1 year ago

@TDtianzhenjiu please ask questions on stackoverflow. Github is for issues