ExecutorService缓慢多线程性能

Gan*_*ngX 6 java concurrency multithreading executorservice executor

我正在尝试执行一个简单的计算(它调用Math.random()10000000次).令人惊讶的是,以简单方法运行它比使用ExecutorService要快得多.

我已经阅读了ExecutorService令人惊讶的性能收支平衡点的另一个主题---经验法则?并尝试通过执行Callable使用批次来遵循答案,但性能仍然很差

如何根据我当前的代码改进性能?

import java.util.*;
import java.util.concurrent.*;

public class MainTest {
    public static void main(String[]args) throws Exception {
        new MainTest().start();;
    }

    final List<Worker> workermulti = new ArrayList<Worker>();
    final List<Worker> workersingle = new ArrayList<Worker>();
    final int count=10000000;

    public void start() throws Exception {
        int n=2;

        workersingle.add(new Worker(1));
        for (int i=0;i<n;i++) {
            // worker will only do count/n job
            workermulti.add(new Worker(n));
        }

        ExecutorService serviceSingle = Executors.newSingleThreadExecutor();
        ExecutorService serviceMulti = Executors.newFixedThreadPool(n);
        long s,e;
        int tests=10;
        List<Long> simple = new ArrayList<Long>();
        List<Long> single = new ArrayList<Long>();
        List<Long> multi = new ArrayList<Long>();

        for (int i=0;i<tests;i++) {
            // simple
            s = System.currentTimeMillis();
            simple();
            e = System.currentTimeMillis();
            simple.add(e-s);

            // single thread
            s = System.currentTimeMillis();
               serviceSingle.invokeAll(workersingle); // single thread
            e = System.currentTimeMillis();
            single.add(e-s);

            // multi thread
            s = System.currentTimeMillis();
               serviceMulti.invokeAll(workermulti);
            e = System.currentTimeMillis();
            multi.add(e-s);
        }
        long avgSimple=sum(simple)/tests;
        long avgSingle=sum(single)/tests;
        long avgMulti=sum(multi)/tests;
        System.out.println("Average simple: "+avgSimple+" ms");
        System.out.println("Average single thread: "+avgSingle+" ms");
        System.out.println("Average multi thread: "+avgMulti+" ms");

        serviceSingle.shutdown();
        serviceMulti.shutdown();
    }

    long sum(List<Long> list) {
        long sum=0;
        for (long l : list) {
            sum+=l;
        }
        return sum;
    }

    private void simple() {
        for (int i=0;i<count;i++){
            Math.random();
        }
    }

    class Worker implements Callable<Void> {
        int n;

        public Worker(int n) {
            this.n=n;
        }

        @Override
        public Void call() throws Exception {
            // divide count with n to perform batch execution
            for (int i=0;i<(count/n);i++) {
                Math.random();
            }
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码的输出

Average simple: 920 ms
Average single thread: 1034 ms
Average multi thread: 1393 ms
Run Code Online (Sandbox Code Playgroud)

编辑:由于Math.random()是一个同步的方法,性能受到影响..用每个线程的新Random对象改变Math.random()后,性能得到改善

新代码的输出(在为每个线程替换Math.random()之后使用Random)

Average simple: 928 ms
Average single thread: 1046 ms
Average multi thread: 642 ms
Run Code Online (Sandbox Code Playgroud)

Rya*_*art 13

Math.random()是同步的.同步的全部意义在于减慢速度,使它们不会发生碰撞.使用未同步的内容和/或为每个线程提供自己的对象,例如新的Random.

  • 我猜是因为你刚回来让多个线程再次竞争相同的资源:在这种情况下是AtomicLong.只有一个线程可以一次更新其值,并且每次调用nextDouble()时都会更新两次. (4认同)