'Break an infinite loop

i have an infinite loop something like that:

while(true) {
    //do something
}

I would like to be able to break the loop using a key for exaple escape key. I think it could be done using events but as i am new to java i cant do that. Can anyone help me? Thank you in advance!

In the while loop the program reads a value from usb and then send it over the network using sockets. My program is the server and sends bytes to a client. I want to be able to stop that server with a key!


In the while loop the program reads a value from usb and then send it over the network using sockets. My program is the server and sends bytes to a client. I want to be able to stop that server with a key!



Solution 1:[1]

Pure java, by itself, does not have a notion of a key input. You usually get them from a specific IO module (e.g., console based, AWT based, Swing based, etc.).

You would then check for the condition or break. If your notification of a press is asynchronous, you would probably set some flag (e.g., breakRequested), and check for this flag and break if it has changed.

For Console access, take a look at http://download.oracle.com/javase/6/docs/api/java/io/Console.html but pay attention to the many questions about this facility here on Stackoverflow.

Solution 2:[2]

Use the break keyword. For example:

try {
  Console console = new Console();
  while(true)
  {
    String input = console.readLine();
    if("quit".equals(input))
    {
       break;
    }
    // otherwise do stuff with input
 }
catch(IOException e)
{
  // handle e
}

Many programmers think the following would be more readable:

try
{
  Console console = new Console();
  for(String input = console.readLine(); !"quit".equals(input); input = console.readLine())
  {
     // do stuff with input
  }
}
catch(IOException e)
{
  // handle e
}

Solution 3:[3]

change while(true) to something like while(!isStopped()){...}

Solution 4:[4]

Keep in mind, while your application is in that block, and is spinning, no other portion of your application will be able to process instructions. That is unless you spin off your worker process into a new thread and share a variable/flag between the two. That way, when a key is pressed, through whatever means you have in capturing it, you can modify a boolean variable which will carry over to your thread. So you can set your loop up like the following:

while(_flag) 
{
   //Spin
}

Solution 5:[5]

I have created this program that terminates an infinite loop if a specific key is pressed:

class terminator {
    public static void main(String args[]) throws java.io.IOException {
        char press, clear;

        for(;;) {
            System.out.print("Press any key to get display, otherwise press t to terminate:  ");
            press = (char)System.in.read();

            System.out.println("You Pressed: "+ press);

            do {
                clear = (char)System.in.read();
            } while(clear != '\n');
            System.out.println("\n");
            if (press=='t'|press=='T') {
                System.out.println("Terminating Program......Program Terminated!");
                break;
            }
        }
    }
}

Solution 6:[6]

To break infinite loop with a condition.

public class WhileInf {

public static void main(String[] args)  {
    int i = 0;

    while (true) {
        if (i == 10) {
            break;
        }
        System.out.println("Hello");
        i++;
    }
}

}

Solution 7:[7]

You can force quit the console if you are in infinite loops. Window = Ctrl + Shift + Esc Mac = Option + Command + Esc

Solution 8:[8]

while(true)
{
   if(   )
   {
        break;
   }
   else if()
   {
        break;
   }
   else
   {
        do something;
        break;
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 bestsss
Solution 4 George Johnston
Solution 5 Edd
Solution 6 yeswanth g
Solution 7 Kyaw Hein Htut
Solution 8 Bo Persson