title: Skip to the next index in a for loop with thread after a given time
body: <p>I'm trying to make a program that creates a vocabulary from a file .txt. It takes a file, creates a document (a String), splits that document in sentences and finally, for each sentence it does some operation (tokenization, pos tagging, costituents tree parsing etc.).</p>
  
  <p>Sometime the program stops on a sentences (that's bad for me), so what I would do is to <strong>avoid to stay on a sentence too time</strong> (let's suppose 120s).
  If so, the program should go to the next sentence in the list.</p>
  
  <p>I thought to use a thread and a timer and check if that time is passed, but I'm confused on how to use the thread. </p>
  
  <p>Here part of my code: </p>
  
  <pre><code>public class Vocabulary {
      private Thread thread;
      private long sentenceDuration;
      private Queue&lt;File&gt; corpus;
  
      public Vocabulary(){
          this.sentenceDuration = 0;           
          corpus = loadCorpus();
      }
  
      public void buildVocabulary(){
  
          for (File f : corpus) {
                  Scanner in = new Scanner(new FileReader(f.getPath()));
  
                  // Create a string representing the document
                  String document = "";
                  while (in.hasNextLine()) {
                      document += in.nextLine() + " ";
                  }
                  in.close();
  
                  // Split the document in sentences
                  ArrayList&lt;String&gt; sentences = tpr.documentToSentences(document);
  
                  for (int i = 0; i &lt; sentences.size(); i++) {
                      Timer timer = new Timer(1000, timer());
                      timer.start();
  
                      while(sentenceDuration &lt; 120){      // while it takes under 120 seconds to compute the sentence, ok.
  
                          List&lt;CoreLabel&gt; tokens = tpr.tokenize(sentences.get(i));        // tokenize the sentence
                          Tree parse = tpr.apply(tokens);                                 // create the costituents tree
                          Object[] basicDependencies = tpr.getBasicDependencies(parse);   // get the basic dependencies from the tree
  
                          // some operations here...             
  
                          Thread.sleep(1000);     // 1000 ms
                      }
  
                      // when it takes over 120 seconds to compute the sentence, jump to the next sentence 
                      System.out.println("Hey, I took long time to compute the sentence. I'm going to the next one!");
                  }  
  
                  // Other operations here...
          }
      }
  
      public void start() {
          end();
          this.thread = new Thread(this);
          this.thread.start();
      }
  
      public void end() {
         if (thread != null &amp;&amp; thread.isAlive()) {
              thread.interrupt();
          }
      }
  
      private ActionListener timer() {
  
          ActionListener taskPerformer = new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  sentenceDuration++;
              }
          };
          return taskPerformer
      }   
  }
  </code></pre>
  
  <p>In the main I simply call:</p>
  
  <pre><code> public static void main(String[] args) {
          new Vocabulary().start();     
   }
  </code></pre>
  
  <p>How can I say to the program: "if 120s are passed, skip the sentence!". That is, "exit the while loop immediatly! No matter what operation you're doing." ? </p>
  
  <p>Thank you!</p>