Infinite Loops using Multithreading

I have a program where I open a .exe file using the Process class in java (and Runtime). The function that does this returns the output of the exe file when a command is sent. Here is the method:

  public String getOutput(int waitTime) {
    StringBuffer buffer = new StringBuffer();
    try {
      Thread.sleep(waitTime);
      sendCommand("isready");
      for (;; ) {
        String text = processReader.readLine();
        if (text.equals("readyok")) {
          break;
        } else {
          buffer.append(text + "\n");
        }
      }
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
    return buffer.toString();
}
  public void sendCommand(String command) {
    try {
      processWriter.write(command + "\n");
      processWriter.flush();
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
  }

Note: I am using outputStreamWriter and BufferedReader to read and write the data that the program outputs.
The only problem is that I have to run this code twice at once, but it pauses the thread making the program unresponsive. Is it possible to implement a second thread that I can use?

Okay, I just found a solution: the thread() method that processing gives. Rather than having to make an entire new thread to use my method, I can make a function that executes the functions that I put inside the first post, and set the thread() method to run that function inside another thread.

1 Like