title: Interrupting a BlockingQueue in a Thread
body: <p>I'm writing a program that has 3 threads. One reads a text file and inputs the words into a ArraylistBlockingQueue of size 2. The next one takes that list and reverses every other word inside of it. The last thread takes the words and writes them to a new text file. </p>
  
  <p>I have everything working except I cannot figure out how to interrupt and stop my threads. The program writes the text file but never ends.</p>
  
  <p>Main method</p>
  
  <pre><code> JFileChooser chooser = new JFileChooser();
      File selectedFile = null;
      File outFile = new File("output.txt");
  
      int returnValue = chooser.showOpenDialog(null);
      if (returnValue == JFileChooser.APPROVE_OPTION) {
          selectedFile = chooser.getSelectedFile();
  
      }
  
  
  
  
      BlockingQueue text = new ArrayBlockingQueue(2);
      BlockingQueue out = new ArrayBlockingQueue(2);
  
      Thread input = new Thread(new inputClass(selectedFile, text));
      Thread reverse = new Thread(new reverseClass(text, out));
      Thread output = new Thread(new outputClass(out, outFile));
  
  
      chooser.setSelectedFile(outFile);
      if (chooser.showSaveDialog(chooser) == JFileChooser.APPROVE_OPTION) {
  
          input.start();
          reverse.start();
          output.start();
  
  
      }
  </code></pre>
  
  <p>Input </p>
  
  <pre><code>@Override
  public void run() {
  
      try {
          Scanner s = new Scanner(f);
  
          while (isRunning) {
  
              try {
                  if(s.hasNext()){
                  text.put(s.next());
                  } else {
                 text.put(END);
              }
              } catch (InterruptedException ex) {
  
                  Logger.getLogger(inputClass.class.getName()).log(Level.SEVERE, null, ex);
              } 
  
          }
  
      } catch (FileNotFoundException ex) {
          Logger.getLogger(inputClass.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>Reverse Class
      @Override
      public void run() {</p>
  
  <pre><code>    String original;
      String temp = "";
      String character = "";
      int count = 1; // keeps track of whether or not a word should be reversed
  
      while (isRunning) {
  
          try {
  
              original = text.take();
              if(original.equalsIgnoreCase(END)){
                  Thread.currentThread().interrupt();
              }
              int length = original.length() - 1;
              //if count is even then the word should be reversed.
              if ((count % 2) == 0) {
                  // reverses the original string if a ? or . appears
                  if (original.contains("?") || original.contains(".")) {
                      character = original.charAt(length) + "";
                      for (int i = (length - 1); i &gt;= 0; i--) {
                          temp = temp + original.charAt(i);
  
                      }
                      out.put(temp + character);
                      temp = "";
                      character = "";
                      // reverses the orgininal string if no ? or . appears
                  } else {
  
                      for (int i = length; i &gt;= 0; i--) {
                          temp = temp + original.charAt(i);
  
                      }
                      out.put(temp);
                      temp = "";
                  }
                  count++;
              } else {
                  out.put(original);
                  count++;
              }
  
  
          } catch (InterruptedException ex) {
  
              Logger.getLogger(reverseClass.class.getName()).log(Level.SEVERE, null, ex);
  
          }
      }
      try {
          out.put(END);
      } catch (InterruptedException ex) {
          Logger.getLogger(reverseClass.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  }
  </code></pre>
  
  <p>Output Code
      @Override
      public void run() {</p>
  
  <pre><code>    while (isRunning) {
  
          String s = null;
          try {
              s = out.take();
          } catch (InterruptedException ex) {
              Logger.getLogger(outputClass.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          try {
  
              BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
  
              try {
                  if (s.equalsIgnoreCase(END)) {
                      Thread.currentThread().interrupt();
                  }
                  writer.write(s + " ");
  
  
              }  finally {
                  writer.flush();
              }
  
          } catch (IOException ex) {
              Logger.getLogger(outputClass.class.getName()).log(Level.SEVERE, null, ex);
  
          }
      }
  </code></pre>