3

I'm writing a simple Java command line program which I need to get user input in non-blocking way.

Typical blocking way would be waiting for user to put some configuration options before connection, but non-blocking would be calling a method in a class while program is running.

My program has a thread running in a while loop, and if I put scanner.nextLine(), the loop will be blocked until user inputs something and press enter.

I want to let the program run normally while user is putting data. I want to trigger a method called updateFreq(double freq) when user puts uf 460.0 and press enter while program is running.

I'm not sure if this behavior makes sense to you.

Do I need to create a separated thread that constantly listen to line input and send signal to the main thread or something?

Thank you in advance.

1 Answer 1

4

Create a new thread, and implement the ability to add a custom listener to communicate events back to your main thread.

freehand code... untested but you get the idea:

class MainThreadClass implements InputListner {
  public MainThreadClass() {
    InputRunner inputRunner = new InputRunner();
    inputRunner.addInputListener(this);
    Thread t = new Thread(new InputThread())
    t.start();
  }

  @Override
  public void inputReceived() {
    //bla bla bla
  }
}

class InputRunner implements Runnable {
  ArrayList<InputListener> listeners = new ArrayList<InputLIsteners>();

  public void run() {
    //code to read input, when it is recieved execute fireInputReceived
  }

  public void addListener(InputListener listener) {
    listeners.add(listener);
  }

  public void fireInputReceived(String input) {
    //loop through all listeners
    //fire inputRecieved on each
  }
}

public Interface InputListener {
  public void inputReceived(String input);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I got back to this late. I will try and let you know! I was thinking to use similar approach, but I wasn't sure if it was "industrial standard" to use a listener or not. :)
To people who searched and ended up here.. DO NOT COPY THIS CODE because there are some syntax errors here. This person provided general concept to use a listener. Please combine this answer with Scanner class. I used while (input.hasNext()) loop to wait for user input in this separated thread. Please comment if you have question.
It is funny that I'm getting downvotes on this 5 years after it was posted... and yes, this code was written freehand and untested (as I pointed out in the answer). It is psuedo code, but pretty close, and should get you in the right direction. It can be copied, but you'll have to fix it and fill in the blanks. Also, yes, there are different and better (debatable) ways to handle events now, but back in the day with AWT/Swing, this was a very common technique.

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.