title: Reading from standard input using java.io.InputStreamReader hangs when process runs in background
body: <p>I'm writing a Java program with multiple threads. One of the threads is responsible for reading lines from the standard input and parsing them.</p>
  
  <p>Everything works well when running normally, but when the program is run in the background (in Linux) using:</p>
  
  <pre><code>$ java -jar my_jar_file &amp;
  </code></pre>
  
  <p>my program hangs (at least until brought to foreground).</p>
  
  <p>When running in the background I don't really need the standard input, but I also don't want my program to hang. </p>
  
  <p>I searched for a way to programatically determine if the process is running in the background but could not find it.</p>
  
  <p>Here's the code that reads from standard input:</p>
  
  <pre><code>InputStreamReader inputStreamReader = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(inputStreamReader);
  
  String line = null;
  
  try {
    while ((line = br.readLine()) != null) {
      parseInputLine(line, br);
    }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  </code></pre>
  
  <p>As far as I can tell, the program hangs when it reaches the <code>br.readLine()</code> command.</p>