是否有任何操作系统设置/选项可能会影响 ExecutorService 的执行方式?
我在不同的操作系统中使用下面的相同代码得到了不同的结果。
ExecutorService es = Executors.newFixedThreadPool(3);
for ( int j = 0 ; j< 1000 ; j ++) {
es.execute(() -> {
String threadName = Thread.currentThread().getName();
long start = System.nanoTime();
System.out.println(threadName + "- start");
System.out.println(threadName + "- end, elapsed time:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS));
});
}
当我从笔记本电脑(Windows)运行此代码时,我得到了如下结果,这是我所期望的。
pool-1-thread-1- start
pool-1-thread-2- start
pool-1-thread-3- start
pool-1-thread-2- end, elapsed time:86
pool-1-thread-1- end, elapsed time:137
pool-1-thread-2- start
pool-1-thread-3- end, elapsed time:155
pool-1-thread-2- end, elapsed time:38
pool-1-thread-1- start
pool-1-thread-2- start
pool-1-thread-3- start
pool-1-thread-2- end, elapsed time:47
pool-1-thread-1- end, elapsed time:91
pool-1-thread-2- start
pool-1-thread-3- end, elapsed time:94
pool-1-thread-2- end, elapsed time:91
问题是当我在 CentOS 服务器上运行相同的代码时,结果完全不同。
pool-1-thread-1- end, elapsed time:5
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:19
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-1- end, elapsed time:4
pool-1-thread-1- start
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:10382
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:4
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:4
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:4
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:5
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:4
pool-1-thread-2- start
pool-1-thread-2- end, elapsed time:4
pool-1-thread-2- start
仅供引用,我没有添加任何 JVM 选项。并且这两个设备具有相同的 jdk 版本。 CentOS 上可能存在一些不寻常的操作系统/内核设置,因为该服务器用于某些特殊目的。但我不熟悉这些设置。
我猜工作线程更改之前的大数字可能是一个提示,但找不到答案。
如有任何建议,我们将不胜感激。
最佳答案
不,任何操作系统上都没有此类设置。 当有空闲的可用处理器时,线程获得时间。 这进一步取决于可用处理器的数量和处理器的速度。
关于java - 任何操作系统设置是否会影响 ExecutorService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51517355/