title: How to interrupt reading on System.in?
body: <p>If I start reading from <code>System.in</code>, it will block the thread until it gets data. There is no way to stop it. Here are all the ways that I've tried:</p>
  
  <ul>
  <li>Interrupting the thread</li>
  <li>Stopping the thread</li>
  <li>Closing <code>System.in</code></li>
  <li>Calling <code>System.exit(0)</code> does indeed stop the thread, but it also kills my application so not ideal.</li>
  <li>Entering a char into the console makes the method return, but I can't rely on user input.</li>
  </ul>
  
  <p>Sample code that does not work:</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException {
      Thread th = new Thread(() -&gt; {
          try {
              System.in.read();
          } catch (IOException e) {
              e.printStackTrace();
          }
      });
      th.start();
      Thread.sleep(1000);
      System.in.close();
      Thread.sleep(1000);
      th.interrupt();
      Thread.sleep(1000);
      th.stop();
      Thread.sleep(1000);
      System.out.println(th.isAlive()); // Outputs true
  }
  </code></pre>
  
  <p>When I run this code, it will output <code>true</code> and run forever.</p>
  
  <p><strong>How do I read from <code>System.in</code> in an interruptible way?</strong></p>