# Infinite Loops using Multithreading

**URL:** <https://discourse.processing.org/t/infinite-loops-using-multithreading/27760>\
**Category:** Coding Questions\
**Created:** [February 12, 2021, 2:34pm UTC](https://discourse.processing.org/t/infinite-loops-using-multithreading/27760 "2021-02-12T14:34:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 12, 2021, 2:34pm UTC](https://discourse.processing.org/t/infinite-loops-using-multithreading/27760/1 "2021-02-12T14:34:00Z")

</div>

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:

```auto
  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?

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 13, 2021, 8:12pm UTC](https://discourse.processing.org/t/infinite-loops-using-multithreading/27760/2 "2021-02-13T20:12:47Z")

</div>

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.
