有一个similar question已经发布在 SO 上,但是,该解决方案仅在循环不生成任何输出时才有效。
相反,如果我尝试运行 while(true) System.out.print(1);
然后点击 Ctrl+C,会发生什么是终端刚刚卡住,似乎没有办法杀死它。
除了关闭和重新打开终端之外还有什么可以做的吗?
最佳答案
你不应该有这样的循环。您应该通过使用
让 Activity 线程 hibernate 一段时间来释放一些时间给 CPUwhile (true) {
// Do your processing stuff here.
// This line ensures that the active thread releases some time
// to the CPU, allowing closure of the application via Ctrl-C.
Thread.sleep(numberOfMillisecondsToSleepFor);
}
您可以使用现有代码从另一个终端使用 kill 终止进程,但您实际上应该通过正确编码循环来防止 CPU 锁定情况。
关于java - 终止无限循环会卡住终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35948783/