我观察到while true sleep(1)Windows 和 macOS/Linux 之间的 CPU 使用率差异很大。
Linux/macOS:下面的代码会导致 CPU 过高,甚至在top. (我的 CPU 有 4 个内核,所以总内核数top是 400%。)
Windows:令我惊讶的是,它在 Windows 任务管理器中只显示 0% 的 CPU 使用率。
那么……原因是什么?
代码有 Java 和 C# 版本;它们显示出相似的结果。
/**
* Platform=openjdk 11, test in CentOS7.5/macOS11/Windows7
*/
public class Test {
public static void main(String[] args) throws Exception {
for (Integer i = 0; i < 100; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try …Run Code Online (Sandbox Code Playgroud)