big zero big zero - 1 year ago 127
Java Question

How to interrupt an Infinite Loop

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)?

Answer Source

I feel dirty even writing this, but...

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download