简体   繁体   English

为什么在单线程、双线程和三线程程序中运行 ExecutorService 需要相同的时间?

[英]Why does running ExecutorService in single, dual and triple threaded program take the same time?

I'm running a Simulation for thin films in very low temperatures.我正在对极低温度下的薄膜进行模拟。 I tried using ExecutorService to properly multithread my code and hopefully reduce runtime.我尝试使用 ExecutorService 正确地对我的代码进行多线程处理,并希望减少运行时间。 But I found out the hard way that this is easier said than done.但我发现这说起来容易做起来难。 I fiddled with many parameters, but wasn't able to increase any efficiency.我摆弄了许多参数,但无法提高任何效率。 What I found weird was that running the code with single, double or triple thread ExecutorService takes almost the same time.我发现奇怪的是,使用单线程、双线程或三线程 ExecutorService 运行代码几乎需要相同的时间。

The bottomline is that although more iteration of the for loop are running parallel at the same time, the amount of time taken to run a single iteration is increased.最重要的是,尽管 for 循环的更多迭代同时并行运行,但运行单次迭代所需的时间却增加了。 The overall total runtime comes out to be almost same no matter how many processors are being utilized.无论使用多少处理器,总运行时间几乎相同。

I'm confused.我糊涂了。 What am I doing wrong?我究竟做错了什么?

public abstract class IsingRun {
public static void main(String[] args)  {   
    long starta = System.currentTimeMillis(); //time for single thread
    for (double t=temp[1];t<=temp[0] ;t+=temp[2] ) {
        long start = System.currentTimeMillis();
        //do stuff
        start = System.currentTimeMillis()-start;
        print(t,output,start); //start is the time taken for single iteration
    }
    starta = System.currentTimeMillis()-starta;
    System.out.println("Total Runtime (Single): "+ starta); //single thread total time

/*end of single threaded process */

    long startb = System.currentTimeMillis();
    ExecutorService e = Executors.newFixedThreadPool(2);
    for (double t=temp[1];t<=temp[0] ;t+=temp[2] ) {
        simulate s = new simulate(t);
        e.execute(s);

    }
    e.shutdown();
    startb = System.currentTimeMillis()-startb;
    System.out.println("Total Runtime (Double): "+ startb);

    /*end of double threaded process */

    long startc = System.currentTimeMillis();
    e = Executors.newFixedThreadPool(3);
    for (double t=temp[1];t<=temp[0] ;t+=temp[2] ) {
        simulate s = new simulate(t);
        e.execute(s);

    }
    e.shutdown();
    startc = System.currentTimeMillis()-startc;
    System.out.println("Total Runtime (Triple): "+ startc);
     /*end of triple threaded process */

}
}
class simulate implements Runnable{
simulate(double T){this.t=T;};

public void run() {
 long start = System.currentTimeMillis();
   //do stuff     
 start = System.currentTimeMillis()-start;
 print(t,output,start); //start is the time taken for single iteration
}
}

I got the following results我得到以下结果

Temp - Output - Runtime for single iteration
2.10 - 0.85410 - 632288
2.20 - 0.83974 - 646527
2.30 - 0.81956 - 655128
2.40 - 0.80318 - 645012
2.50 - 0.79169 - 649863
2.60 - 0.77140 - 662429
Total Runtime (Single): 3891257
2.10 - 0.85585 - 1291943
2.20 - 0.83733 - 1299240
2.40 - 0.80284 - 1313495
2.30 - 0.82294 - 1334043
2.50 - 0.79098 - 1315072
2.60 - 0.77341 - 1338203
Total Runtime (Double): 3964290
2.10 - 0.85001 - 1954315
2.20 - 0.84137 - 1971372
2.30 - 0.82196 - 1996214
2.40 - 0.80684 - 1966009
2.50 - 0.78995 - 1970542
2.60 - 0.77437 - 1966503
Total Runtime (Triple): 3962763

What am I doing wrong?我究竟做错了什么? Task manager show all processors are being used, but is it?任务管理器显示所有处理器都在使用中,但是是吗?

A couple of things:几件事:

1) If you take a look at the ExecutorService Javadoc , the shutdown() method does not wait for threads to complete execution. 1) 如果您查看ExecutorService Javadoc ,则 shutdown() 方法不会等待线程完成执行。 In you code you are not waiting for the task to actually complete.在您的代码中,您不是在等待任务实际完成。 The javadoc also contains some sample code which demonstrates how to properly wait for ExecutorService to finish. javadoc 还包含一些示例代码,演示如何正确等待 ExecutorService 完成。

2) Benchmarking programs is not really a straight forward thing. 2)基准测试程序并不是一件简单的事情。 I may/may not trust the numbers here.我可能/可能不相信这里的数字。 Also take a look at this SO post regarding measuring execution time in Java.另请查看有关在 Java 中测量执行时间的SO 帖子 Also, I would test your different type of execution methods separately and not all in one go.此外,我会分别测试您不同类型的执行方法,而不是一次性测试所有方法。

3) If your task is really CPU intensive. 3) 如果您的任务确实是 CPU 密集型的。 I would create a ExecutorService with the number of threads matching the number of cores/processors in your machine.我会创建一个 ExecutorService ,其线程数与您机器中的内核/处理器数相匹配。 If your tasks are short lived then the additional overhead of threading/context switching may not be worth it and single threaded might be the way to go.如果您的任务是短暂的,那么线程/上下文切换的额外开销可能不值得,单线程可能是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么多线程版本的时间与单线程版本相同? - Why does multithreaded version take the same amount of time as single threaded version? Java ExecutorService-为什么该程序保持运行? - Java ExecutorService - why does this program keep running? 为什么运行单线程 Java 程序会导致许多内核处于活动状态? - Why does running a single threaded Java program result in many cores active? 为什么我的 ExecutorService 实现比单线程实现更差? - Why is my ExecutorService implementation performing worse than single threaded implementation? 为什么一个单线程的Java程序有这么多线程? - Why does a single threaded java program have so many threads? 为什么单线程程序不会发生活性失败? - Why liveness failure does not occur in single-threaded program? 为什么这个多线程并行程序执行接近顺序时间? - Why does this multi threaded parallel program executes close to sequential time? RxJava中的单线程ExecutorService等效于什么? - What is the equivalent of a single threaded ExecutorService in RxJava? 为什么这个简单的线程程序会卡住? - Why does this simple threaded program get stuck? 为什么单线程操作比无线程操作花费的时间长 3 倍? - Why does a single threaded operationg takes 3 times longer than no threaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM