title: Java: how to abort a thread reading from System.in
body: <p>I have a Java thread:</p>
  
  <pre><code>class MyThread extends Thread {
    @Override
    public void run() {
      BufferedReader stdin =
          new BufferedReader(new InputStreamReader(System.in));
      String msg;
      try {
        while ((msg = stdin.readLine()) != null) {
          System.out.println("Got: " + msg);
        }
        System.out.println("Aborted.");
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In another thread, how do I abort the <code>stdin.readline()</code> call in this thread, so that this thread prints <code>Aborted.</code>? I have tried <code>System.in.close()</code>, but that doesn't make any difference, <code>stdin.readline()</code> is still blocking.</p>
  
  <p>I'm interested in solutions without</p>
  
  <ul>
  <li>busy waiting (because that burns 100% CPU);</li>
  <li>sleeping (because then the program doesn't respond instantly to <code>System.in</code>).</li>
  </ul>