gpt4 book ai didi

Java线程启动延迟

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:58 24 4
gpt4 key购买 nike

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

最佳答案

在 executorservice 上调用 invokeAll 后,无法保证所有任务的执行将同时开始。线程调度是高度操作系统特定的,你唯一可以依赖的是你不能依赖诸如“线程何时真正启动”、“它将运行多长时间”之类的东西......以Java的线程状态“Runnable”为例:

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

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

关于Java线程启动延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com