Java - multithreaded code does not run faster on more cores

后端 未结 5 1652
半阙折子戏
半阙折子戏 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:07

    Here is a not tested SpinBarrier but it should work.

    Check if that may have any improvement on the case. Since you run the code in loop extra sync only hurt performance if you have the cores on idle. Btw, I still believe you have a bug in the calc, memory intense operation. Can you tell what CPU+OS you use.

    Edit, forgot the version out.

    import java.util.concurrent.atomic.AtomicInteger;
    
    public class SpinBarrier {
        final int permits;
        final AtomicInteger count;
        final AtomicInteger version;
        public SpinBarrier(int count){ 
            this.count = new AtomicInteger(count);
            this.permits= count;
            this.version = new AtomicInteger();
        }
    
        public void await(){        
            for (int c = count.decrementAndGet(), v = this.version.get(); c!=0 && v==version.get(); c=count.get()){
                spinWait();
            }       
            if (count.compareAndSet(0, permits)){;//only one succeeds here, the rest will lose the CAS
                this.version.incrementAndGet();
            }
        }
    
        protected void spinWait() {
        }
    }
    

提交回复
热议问题