为什么没有“Thread.sleep”的“while(true)”会导致Linux上的CPU使用率达到100%而Windows却没有? [英] Why does “while(true)” without “Thread.sleep” cause 100% CPU usage on Linux but not on Windows?

查看:473
本文介绍了为什么没有“Thread.sleep”的“while(true)”会导致Linux上的CPU使用率达到100%而Windows却没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中创建了一个简单的程序:

  public static void main(String [] args)throws InterruptedException {
而(true)
;
}

如果我在Linux机器上运行它,它显示100%的CPU使用率,但不会导致操作系统显得缓慢。但是,如果我在Windows上运行完全相同的代码,它只显示大约20%的CPU使用率。



我在Windows上使用Oracle JRE,在Linux上使用OpenJDK 6。 / p>

我想知道Windows的调度程序是否随机抢占线程而Linux是不是?

解决方案

默认情况下,Linux上的 top 在所谓的IRIX模式下运行,而Windows任务管理器则没有。假设您有4个核心:




  • 启用IRIX模式后,1个完全利用的核心为100%,4个核心为400% 。


  • 关闭IRIX模式后,1个完全使用的核心为25%,4个核心为100%。




这意味着默认情况下,Linux上的 top 将显示无限循环为~100%,Windows将其显示为~25%,这意味着完全相同。



您可以在<$ c时切换IRIX模式$ c> top 正在使用 Shift + i 运行。这将使数字匹配。


I have created a simple program in java:

public static void main(String[] args) throws InterruptedException {
    while (true) 
        ;
}

If I run this on a Linux machine, it shows 100% CPU usage, but doesn't cause the OS to appear slow. However, if I run the exact same code on Windows, it only shows about 20% CPU usage.

I am using Oracle JRE on Windows and OpenJDK 6 on Linux.

I'm wondering if Windows' scheduler preempt threads randomly and Linux's doesn't?

解决方案

By default, top on Linux runs in so-called IRIX mode, while the Windows Task Manager does not. Let's say you have 4 cores:

  • With IRIX mode on, 1 fully utilized core is 100% and 4 cores are 400%.

  • With IRIX mode off, 1 fully utilized core is 25% and 4 cores are 100%.

This means that by default, top on Linux will show an infinite loop as ~100% and Windows will show it as ~25%, and it means exactly the same thing.

You can toggle IRIX mode while top is running with Shift+i. This will make the numbers match up.

这篇关于为什么没有“Thread.sleep”的“while(true)”会导致Linux上的CPU使用率达到100%而Windows却没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆