java - 为什么 Windows 和 macOS/Linux 之间的 sleep(1) 的 CPU 使用率存在很大差异?
问题描述
我观察到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 {
while (true) {
Thread.sleep(1);
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
});
thread.start();
if(i == 100)
thread.join();
}
}
}
C#
/**
* sdk=NET5, test in CentOS7.5/macOS11/Windows7
*/
class Program
{
static void Main(string[] args)
{
var threadList = new List<Thread>(100);
foreach (var index in Enumerable.Range(1, 100))
{
var thread = new Thread(() =>
{
while (true)
{
Thread.Sleep(1);
}
});
thread.Start();
threadList.Add(thread);
}
threadList[0].Join();
}
}
2021.5.8
我已经实现了这个演示的 goroutine 版本,它在 CentOS7 上只使用了不到 5% 的 CPU 使用率。那么我可以认为CPU使用率被Linux LWP的上下文切换消耗了吗?
去
package main
import (
"runtime"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
for i := 0; i < 100; i++ {
go sleep()
}
runtime.Gosched()
wg.Add(1)
wg.Wait()
}
func sleep() {
for {
time.Sleep(time.Millisecond)
}
}
解决方案
推荐阅读
- javascript - 无法使用 webpack 4 和 babel 7 在我的 react-application 中导入 jsx 模块
- kdb - q kdb 中 iasc 与 rank 的关系
- algolia - Algolia 搜索包含值的数组
- jira - 在 JIRA 中的已解决票证中格式化解决方案详细信息
- google-apps-script - 创建添加 BottomBar 的 Google App Scripts 插件
- arrays - 解释这个for循环在python中的工作原理
- c# - 社交分享授权页面
- c++ - 如何使用移位运算符分配 BH1750 光传感器中的值 <
我正在使用带有 arduino 的 BH1750 光传感器在 LCD 上显示勒克斯值。但在代码中,我无法得到公式。谁能给我解释一下公式?
我试图在 BH1750 的数据表中找到它,但我无法理解这一行:
value = ((buff[0]<<8)|buff[1])/1.2;
整个代码:
- google-apps - 用户与许可 Google Apps 免费版
- reactjs - 重定向无法正常工作。它不断重新加载页面