首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java线程启动延迟

Java线程启动延迟
EN

Stack Overflow用户
提问于 2018-09-24 04:44:44
回答 1查看 251关注 0票数 2

我使用JAVA ExecutorService来并行我的任务。有48个可用的处理器,每个处理器有12个核心。所以我假设我可以同时启动500个以上的线程(500 > 12 * 48)。

这里是详细的信息

代码语言:javascript
复制
    processor   : 47
    vendor_id   : GenuineIntel
    cpu family  : 6
    model       : 85
    model name  : Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz
    stepping    : 4
    microcode   : 0x2000043
    cpu MHz     : 999.985
    cache size  : 16896 KB
    physical id : 1
    siblings    : 24
    core id     : 11
    cpu cores   : 12
    apicid      : 55
    initial apicid  : 55
    fpu     : yes
    fpu_exception   : yes
    cpuid level : 22
    wp      : yes

我试着并行下面的任务。代码看起来像这样,

代码语言:javascript
复制
    int numThread = 500;
    ExecutorService executorService = Executors.newFixedThreadPool(numThread);
    long startParallel = System.currentTimeMillis();
    List<Callable<List<Integer>>> callableTasks = new ArrayList<>();
    for (int i = 0; i < numThread; i++) {
        final int startTupleIndex = i * batchSize;
        final int endTupleIndex = (i + 1) * batchSize;
        callableTasks.add(() -> {
            List<Integer> batchResult = new ArrayList<>();
            long start = System.currentTimeMillis();
            for (int j = startTupleIndex; j < endTupleIndex; j++) {
                //Some evaluation function
            }
            long end = System.currentTimeMillis();
            System.out.println("index from "+startTupleIndex+" to "+endTupleIndex+" ,time from "
                    +start+" to "+end+", duration " + (end - start) +"ms");
            return batchResult;
        });
    }


    List<Future<List<Integer>>> results = executorService.invokeAll(callableTasks);
    for (Future<List<Integer>> result : results)
        validTuples.addAll(result.get());
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    executorService.shutdown();
    long endParallel = System.currentTimeMillis();
    System.out.println("total ms:"+ (endParallel - startParallel));

然而,不同线程的开始时间差别很大。

代码语言:javascript
复制
    index from 144978 to 217467 ,time from 1537729372817 to 1537729372984, duration 167ms
    index from 1159824 to 1232313 ,time from 1537729372819 to 1537729372985, duration 166ms
    index from 1377291 to 1449780 ,time from 1537729372819 to 1537729372987, duration 168ms
    index from 434934 to 507423 ,time from 1537729372818 to 1537729372990, duration 172ms
    index from 942357 to 1014846 ,time from 1537729372819 to 1537729372992, duration 173ms
    index from 579912 to 652401 ,time from 1537729372818 to 1537729372993, duration 175ms
    index from 724890 to 797379 ,time from 1537729372818 to 1537729372996, duration 178ms
    ...
    index from 34794720 to 34867209 ,time from 1537729374532 to 1537729374733, duration 201ms
    index from 36099522 to 36172011 ,time from 1537729374627 to 1537729374733, duration 106ms
    index from 36172011 to 36244344 ,time from 1537729374628 to 1537729374734, duration 106ms
    index from 35302143 to 35374632 ,time from 1537729374563 to 1537729374741, duration 178ms
    index from 35882055 to 35954544 ,time from 1537729374619 to 1537729374741, duration 122ms
    index from 35447121 to 35519610 ,time from 1537729374579 to 1537729374742, duration 163ms
    index from 35157165 to 35229654 ,time from 1537729374551 to 1537729374745, duration 194ms
    index from 35737077 to 35809566 ,time from 1537729374595 to 1537729374746, duration 151ms
    index from 35519610 to 35592099 ,time from 1537729374594 to 1537729374747, duration 153ms
    index from 35374632 to 35447121 ,time from 1537729374587 to 1537729374747, duration 160ms
    index from 35664588 to 35737077 ,time from 1537729374595 to 1537729374748, duration 153ms
    index from 35592099 to 35664588 ,time from 1537729374595 to 1537729374751, duration 156ms
    index from 35809566 to 35882055 ,time from 1537729374619 to 1537729374752, duration 133ms
    total ms:1939.

问题是每个线程大约完成200ms,但总运行时间超过1900ms。任务开始时间大致在1537729372817到1537729374628之间。但是有超过500个可用的物理核心。为什么这些任务不能同时启动?非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2018-09-24 05:06:08

在executorservice上调用invokeAll之后,不能保证所有任务的执行都会同时开始。线程调度是高度特定于操作系统的,你唯一可以依赖的是你不能依赖“线程什么时候真正开始”,“它会运行多长时间”之类的东西。以Java的线程状态‘Runnable’为例:

“准备运行的线程被转移到runnable状态。在这种状态下,线程可能实际正在运行,也可能随时准备运行。线程调度程序有责任为线程提供运行时间。

https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52470104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档