如何中断无限循环 [英] How to interrupt an Infinite Loop

查看:169
本文介绍了如何中断无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道提问会有点傻,但我还是想更多地了解它的技术观点。

Though I know it'll be a bit silly to ask, still I want to inquire more about the technical perspective of it.

无限的一个简单例子循环:

A simple example of an infinite loop:

public class LoopInfinite {
    public static void main(String[] args) {
        for (;;) {
            System.out.println("Stack Overflow");
        }
    }
}

如何中断(停止) )来自这个类之外的这个无限循环(例如,借助于继承)?

How can I interrupt (stop) this infinite loop from outside of this class (e.g., with the help of inheritance)?

推荐答案

即使写这个也感觉很脏,但是......

I feel dirty even writing this, but...

从另一个线程,您可以使用<$调用 System.setOut() c $ c> PrintStream 实现,当你调用 println()时会抛出 RuntimeException

From a different thread, you could call System.setOut() with a PrintStream implementation, which throws a RuntimeException when you call println().

这篇关于如何中断无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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