Java 线程启动延迟



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

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

"准备运行的线程将移动到可运行状态。在此状态下,线程可能实际上正在运行,或者可能随时准备好运行。线程调度程序负责为线程提供运行时间。

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

最新更新