Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interrupt an Infinite Loop

Tags:

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

like image 356
big zero Avatar asked Aug 17 '12 08:08

big zero


People also ask

How do you break an infinite loop?

You can press Ctrl + C .

Can an infinite loop be broken?

An infinite loop will run indefinitely, until it is explicitly broken out of using either a break , exit or raise statement.


1 Answers

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().

like image 101
David Grant Avatar answered Sep 22 '22 01:09

David Grant