title: How interrupt/stop a thread in Java?
body: <p>I'm trying to stop a thread but I can't do that :</p>
  
  <pre><code>public class Middleware {
  
  public void read() {
      try {
          socket = new Socket("192.168.1.8", 2001);
  
          // code .. 
  
          Scan scan = new Scan();
          thread = new Thread(scan);
          thread.start();
  
      } catch (UnknownHostException ex) {
          ex.printStackTrace();
  
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  }
  
  class Scan extends Thread {
  
      public void run() {
  
          while (true) {
              try {
              // my code goes here
  
              } catch (IOException ex) {
                  thread.currentThread().interrupt();
              }
          }
      }
  }
  
  public void stop() {
      Thread.currentThread().interrupt();
  }
  
  // get and setters
  }
  </code></pre>
  
  <p>So, even when i call the method 'stop' the thread don't stop.
  It keeps alive.</p>
  
  <p>How can I interrupt/stop this thread ?</p>
  
  <p><strong>UPDATE</strong>
  (@little approach)</p>
  
  <pre><code>private void tb_startActionPerformed(java.awt.event.ActionEvent evt) {                                         
      Middleware middleware = new Middleware();
  
      if (tb_start.getText().equals("Start")){
          tb_start.setText("Stop");
  
          // starting to read rfid tags
          middleware.read();
      }else{
          tb_start.setText("Start");
  
          // stop reading rfid tags
          middleware.stop();
      }
  }
  </code></pre>
  
  <p>The Middleware class :</p>
  
  <pre><code>public class Middleware {
  
      private Scan scan;
  
      public void read() {
  
          scan = new Scan();
          scan.start();
      }
  
      private class Scan extends Thread {
  
          @Override
          public void run() {
              while (!Thread.currentThread().isInterrupted()) {
                  System.out.println("reading...");
              }
          }
      }
  
      public void stop() {
          if (scan != null) {
              scan.interrupt();
          }
      }
  }
  </code></pre>
  
  <p>But when I try to stop the thread, it doesn't.</p>
  
  <p>What could be wrong in the code above ?</p>