title: How to stop waiting for user input?
body: <p>I'm building a programm to ask multiplication and I want to set up a timer to force the person to give its answer in a given time : </p>
  
  <ol>
  <li>if the person answers before the end of the timer : go next multiplication</li>
  <li>if the timer reach its end, stop waiting user input : go next multiplication</li>
  </ol>
  
  <p>For the moment, case <code>1</code> can be done, but <code>2</code> not, I was thinking about a way to <code>return;</code> from the method within like a Thread or something, bu I don't know how</p>
  
  <p>So I'm facing a problem, if a <code>Scanner</code> is open, waiting for input, how to stop it ? I've tried putting it in a Thread and <code>interrupt()</code> it or using <code>boolean</code> as flags, but it doesn't stop the <code>Scanner</code></p>
  
  <hr>
  
  <pre><code>class Multiplication extends Calcul {    
      Multiplication() {  super((nb1, nb2) -&gt; nb1 * nb2); }   
      @Override
      public String toString() {  return getNb1() + "*" + getNb2(); }
  }
  
  
  abstract class Calcul {
  
      private int nb1, nb2;
      private boolean valid;
      private boolean inTime = true;
      private boolean answered = false;
      private BiFunction&lt;Integer, Integer, Integer&gt; function;
  
      Calcul(BiFunction&lt;Integer, Integer, Integer&gt; f) {
          this.nb1 = new Random().nextInt(11);
          this.nb2 = new Random().nextInt(11);
          this.function = f;
      }
  
      void start() {
          Scanner sc = new Scanner(System.in);
          System.out.println("What much is " + this + " ?");
  
          Timer timer = new Timer();
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  if (!answered) {
                      inTime = false;
                  }
              }
          }, 5 * 1000);
  
          int answer = Integer.parseInt(sc.nextLine());
          if (inTime) {
              checkAnswer(answer);
              timer.cancel();
          }
      }    
  
      private void checkAnswer(int answer) {
          System.out.println("You said " + answer);
          valid = (function.apply(nb1, nb2) == answer) &amp;&amp; inTime;
          answered = true;
      }
  
      int getNb1() {   return nb1;  }    
      int getNb2() {   return nb2;  }    
      boolean isValid() { return valid; }
  
       public static void main(String[] args) {
           List&lt;Calcul&gt; l = Arrays.asList(new Multiplication(), new Multiplication(), new Multiplication());
           l.forEach(Calcul::start);
  }
  }
  </code></pre>