24

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

5
  • 12
    pull the plug on the jvm :P Commented Aug 17, 2012 at 10:54
  • 18
    Have you tried turning it off and on again? Commented Aug 17, 2012 at 13:54
  • 1
    This reminds me of a certain Kōan, Interrupt Commented Aug 17, 2012 at 17:00
  • 3
    Well, first you should write something that decides if this loop (or any other) will run forever or eventually halt... Commented Aug 17, 2012 at 18:04
  • If it's infinite, then by definition it is not interrupted :-P Commented Aug 18, 2012 at 8:18

12 Answers 12

49

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

Sign up to request clarification or add additional context in comments.

16 Comments

System.setOut(null) would suffice.
@MarkoTopolnik Maybe another class with a main method just called the LoopInfinite.main(null) or something like that. We don't know, only the OP knows...
@maba That' a VERY far-fetched hypothesis. When an asker posts code involving main, we can be pretty sure he means that's his entry point. Otherwise, why not assume there's an AOP infrastructure that would allow injecting arbitrary code to every PrintStream.println call, or that System is actually an import my.package.System with a completely unrelated implementation?
@Marko I think you're probably looking into the method issue too much. Why not post the Thread.stop as a different answer, including the Thread access.
@Marko I hope for the sake of all that is sacred in the programming world that this is a purely academic question. ;)
|
26

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;
      }
}

9 Comments

Don't forget to declare cancelled as volatile
Maybe you would want the cancel() method to be static too. Then you don't need an object of LoopInfinite to be able to stop the loop.
@maba - dude u r r8 in this context. This is a snippet not actual implementation. probably there may be thousand changes required for actual implementation.
@Buhb: That's not necessary unless you have other memory being read/written to from multiple threads that has to be consistent with the 'cancelled' flag. And if that's your scenario, you're better off using locks/Monitor or other heavyweight concurrency control, because 'volatile' by itself is probably not going to help you. And if you do use locks properly, volatile becomes unnecessary in the majority of cases. Avoid it unless you really understand what you're doing, down to the level of the CLR memory model and what store/release reordering is. Just use locks.
Why use any local variables for this, rather than the native interrupt()-support? Simply writing while(!isInterrupted()) will continue until someone calls the interrupt()-method on the thread.
|
6

You can get at the thread running the infinite loop from a different thread and call interrupt on it. You'll have to be very sure what you are doing though, and hope that the interrupted thread will behave properly when interrupted.

Here, I've named the thread with the offending loop for easier identification. Beware that the following solution is vulnerable to race conditions.

    Thread loop = new Thread() { 

        public void run() {
            Thread.currentThread().setName("loop");
            while(true) {
                System.out.print(".");
            }
        }
    }.start();

Then in some other class:

    ThreadGroup group = Thread.currentThread().getThreadGroup();
    Thread[] threads = new Thread[group.activeCount()];
    group.enumerate(threads);

    for(Thread t : threads) {
        if(t.getName().equals("loop")) {
            /* Thread.stop() is a horrible thing to use. 
               Use Thread.interrupt() instead if you have 
               any control over the running thread */
            t.stop();
        }
    }

Note that in my example I assume the two threads are in the same ThreadGroup. There is no guarantee that this will be the case, so you might need to traverse more groups.

If you have some control over this, a decent pattern here would be to have while(!isInterrupted()) instead in the loop declaration and use t.interrupt() instead of t.stop().

My only advice to you, even after posting this, is to not do this. You can do it, but you really shouldn't.

3 Comments

When you call Thread.currentThread().setName("loop");, aren't you actually still in the main loop and setting a new name for it? I don't usually override the start() method of the Thread class.
I am not sure that your loop will acutally stop. In the loop you will have to do if (Thread.interrupted()) break;. You can read about interruption of threads here: docs.oracle.com/javase/tutorial/essential/concurrency/…
Modified to use Thread.stop() instead, since you're right. Wishful thinking on my part to think I could avoid stop() ;)
5

I think this is not possible. Only using break within the loop. You could use

while(cond) {}

And from some other place make it false

Comments

3

You can interrupt this thread by keeping its static reference of inherited reference to this Thread [main] by asking from Thread.currentThread(), like this

public class LoopInfinite{
public static Thread main = null;
public static void main(String[] args){
    main = Thread.currentThread();
    for(;;)
       System.out.println("Stackoverflow");
    }
}

And to terminate you can call this from some other thread

LoopInfinite.main.interrupt();

But it will only work if both threads are part of the same group. Otherwise calling thread will get SecurityException

Comments

2

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.

2 Comments

You can't use inheritance in this case because it's a static method.
I mean,that in a simple class this infinite loop code is written,then inwhich way I can stop this infinite funtion
2

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.

Comments

2

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.

2 Comments

for the sake of simplicity,let say,I'm not usin main();in a simple class just this piece of infinite code is written,...then
Then if you can't change the code, you'll have to find the Thread it's running in, which could either be by having a reference to it from when it was created, or you could loop through all the Threads there are and try to determine which is the appropriate one, but this might be difficult. If you specify more clearly what exactly the situation is (how is the loop started, what exactly do you need to happen, etc., for example, is the loop started by code you control?)
2

Your kind of problem looks like a Threading problem. But still, it is now a a good practice to include a stopping flag even in threads

Comments

2

If you need an "infinite" loop, you sure need a thread (else your app will be stuck until the end of the loop).

class BigLoop extends Thread
{

    private boolean _sexyAndAlive = true;

    // make some constructor !

    public void softTerminate()
    {
    _sexyAndAlive = false;
    }


    public void run()
    {
        try
        {
            while( _sexyAndAlive )
            {
               // Put your code here 
            }
        }
        catch( Some Exceptions ... )
        {
            // ...
        }
        // put some ending code here if needed
    }
}


// in another file :

BigLoop worker = new BigLoop();
worker.start(); // starts the thread

// when you want to stop it softly
worker.softTerminate();

So, this is a simple method to have background running loop.

1 Comment

You can also, if you know approximately how much loops are needed (eg. a few thousands), set a max count : int i=0, maxCount=10000; while(true){doSomething();i++;if(i>maxCount){break;}} // So you will never go beyond 10k loops
1

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;
    }
}

}

Comments

0

Here is what I did:

while(Exit == false){
    Scanner input = new Scanner(System.in);
    String in = input.next();

    switch(in){
        case "FindH": 
            FindHyp hyp = new FindHyp();
            float output = hyp.findhyp();
            System.out.println(output); 
        case "Exit":
            Exit = true;
    break;

    }
  }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.