gpt4 book ai didi

java - java中没有任何代码的情况下无限运行while循环

转载 作者:行者123 更新时间:2023-11-30 05:50:39 27 4
gpt4 key购买 nike

假设我写了一个无限写循环,但里面没有语句?它会产生任何问题,例如内存已满等或 JVM 将在一段时间后停止响应吗?

最佳答案

你为什么要这么做?

回答一下,它不会消耗无尽的内存,但 CPU 使用率可能会很痛苦,因为实际上根本没有指令。

至少,您应该帮助 CPU 抢占,从而允许线程让出:

Thread.yield();

您可以在 Java Api Javadoc 中阅读此内容:

A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.

关于java - java中没有任何代码的情况下无限运行while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53964094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com