java - 任何操作系统设置是否会影响 ExecutorService?

标签 java multithreading linux-kernel executorservice

是否有任何操作系统设置/选项可能会影响 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/

相关文章:

java - 如何在不阻塞的情况下使用设定数量的 Java/Groovy 线程执行任务池

eclipse - 64 位 Windows 7 上的 32 位 Java,用于在 Eclipse Juno 中运行 JBoss JSP 编辑器

java - 在Java线程应用程序中获取不正确的输出? [复制]

Java 生产者消费者 - 为什么不是一个简单的固定线程池?

linux - 为什么我在使用 linux hrtimer 进行一秒回调时会出现时间漂移?

java - 如何在二进制中打印 Long?

java - H2 DB 校对

multithreading - 如何向单个客户端而不是所有客户端发送命令?

c - 将更改恢复到内核模块中的文件

multithreading - Linux中的内核线程退出