Java线程启动延迟

时间:2018-09-23 20:44:44

标签: java multithreading executorservice

我使用JAVA ExecutorService并行执行任务。共有48个可用处理器,每个处理器具有12个内核。因此,我想我可以同时启动500多个线程(500> 12 * 48)。

这是详细信息

    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

我尝试并行执行以下任务。代码看起来像

    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));

但是,不同线程的开始时间相差很大。

    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多个可用的物理核心。为什么这些任务不能同时开始?非常感谢。

1 个答案:

答案 0 :(得分:0)

在executorservice上调用invokeAll之后,不能保证所有任务的执行将同时开始。线程调度是高度特定于OS的,您唯一可以依靠的就是您不能依赖于诸如“线程何时真正启动”,“它将运行多长时间”之类的事情……例如以Java的线程状态“ Runnable”为例:

“已准备好运行的线程进入了可运行状态。在这种状态下,线程可能实际上正在运行,或者随时可能准备好运行。这是线程调度程序的责任。赋予线程运行时间。

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

相关问题