3

So I have an infinite loop running in the main thread of my console application like so.

public static void main(String[] args) {
    while(true){
        doSomething();
        doSomethingElse();
        System.out.println("some debugging info");
        doAnotherThing();
    }
}

I want this code to run over and over and over.

Once in a while, I would like to input a command into the console, such as the string "give me more info plox", and then if that command equals something, I want my code to do something.

Normally I would just use a scanner, but I can't do that here - since scanner.next(); pauses my code... I want my code to keep running whether or not I enter a command. The only workaround I can see is by using a file. But is there any other option?

1
  • 1
    Hint: you don't want to repeat this things infinitely, you just want it to WAIT until something happens. Commented Jun 26, 2013 at 23:37

3 Answers 3

3

Use threads, a main thread to read from console and another one to do the loop, the first thread updates a list of strings (producer) and the loop thread reads the list to see if there is something new for it (consumer)

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

Comments

3

You may do something like below

public class MainApp implements Runnable
{

    static String command;
    static boolean newCommand = false;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        MainApp reader = new MainApp();
        Thread t = new Thread(reader);
        t.start();

        while (true)
        {
            doSomething();

            if (newCommand)
            {
                System.out.println("command: " + command);
                newCommand = false;
                //compare command here and do something
            }
        }
    }

    private static void doSomething()
    {
        try
        {
            System.out.println("going to do some work");
            Thread.sleep(2000);
        } catch (InterruptedException ex)
        {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        Scanner scanner = new Scanner(System.in);

        while(true)
        {
            command = scanner.nextLine();
            System.out.println("Input: " + command);
            newCommand= true;
        }
    }



}

Comments

0

You could try System.in.available(), it is non-blocking. However, the method is known as not well spec'ed. On some systems (Unix based OpenJDK) it only returns > 0 after the user has confirmed the input with the enter key.

Otherwise, morgano's suggestion to continuously block for System.in.read() on a separate thread.

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.