How to interrupt an Infinite Loop

前端 未结 12 1843
自闭症患者
自闭症患者 2021-02-02 10:36

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:

pub         


        
相关标签:
12条回答
  • 2021-02-02 10:43

    You cannot stop this from outside of this class. If you use inheritance you can overwrite your loop, but without abort-flag you won't be able to do so.

    0 讨论(0)
  • 2021-02-02 10:45

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

    0 讨论(0)
  • 2021-02-02 10:46

    Very open question, but stopping such loop would most likely require you to operate from another thread. The other thread would then need to set some variable that your infinite loop can check regularly, and if the variable has a certain value; break out of the loop.

    0 讨论(0)
  • 2021-02-02 10:47

    We can achieve it using volatile variable, which we will change ouside Thread and stop the loop.

       for(;!cancelled;) /*or while(!cancelled)*/{
           System.out.println("Stackoverflow");
       }
    

    This is better way to write Infinite Loop.

    public class LoopInfinite{
          private static volatile boolean cancelled=false;
          public static void main(String[] args){
                 for(;!cancelled;) { //or while(!cancelled)
                        System.out.println("Stackoverflow");
                 }
          }
          public void cancel(){
            cancelled=true;
          }
    }
    
    0 讨论(0)
  • 2021-02-02 10:47

    You won't be able to interrupt this particular loop without halting the process entirely. In general, if you're trying to do it from an external source (I'm assuming you have no control over the source code, because if you did you could easily set a condition in the loop, such as a boolean you could set from an external Thread), you will have to halt the running Thread, whether you do this through the Thread object (you'll have to find a reference to it somehow, for example by looping through existing Threads), or whether you halt it as a system process.

    Another option would be to override the method with a loop that isn't an infinite loop, but unfortunately that doesn't apply to your example because it's a static method.

    0 讨论(0)
  • 2021-02-02 10:51

    Add a variable shouldBreak or something which can be set using getter and setter.

    public class LoopInfinite {
    private boolean shouldBreak = false;
    
    public boolean isShouldBreak() {
        return shouldBreak;
    }
    
    public void setShouldBreak(boolean shouldBreak) {
        this.shouldBreak = shouldBreak;
    }
    
    public static void main(String[] args) {
        // Below code is just to simulate how it can be done from out side of
        // the class
        LoopInfinite infinite = new LoopInfinite();
        infinite.setShouldBreak(true);
        for (;;) {
            System.out.println("Stackoverflow");
            if (infinite.shouldBreak)
                break;
        }
    }
    

    }

    0 讨论(0)
提交回复
热议问题