title: Making a countdown timer that doesn't interrupt the entire program?
body: <p>So I'm trying to make a reaction game where you press start button, a hidden timer that will countdown to zero from a random number between 1 &amp; 10 seconds. Most answer regarding java timers recommend using </p>
  
  <blockquote>
    <p>Thread.sleep(1000);</p>
  </blockquote>
  
  <p>However this interrupts the entire program while I just wants it to countdown. How should I solve this? </p>
  
  <p>After pressing start and the program has counted down from a random number. The blue icon (entire code below) will turn red and then you're supposed to press it and it will display the time it took for you to press it. </p>
  
  <p>Code is focus:</p>
  
  <pre><code> public void countDown() throws InterruptedException {
  
      int random = r.nextInt((10000 - 1000) + 1) + 1000;
  
      while(random &gt;= 0){
          Thread.sleep(1000);
          random -= 1000;
      }
  
      if (random &lt;= 0) {
          button_1.setIcon(img_react);
          repaint();
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Image files used: </p>
  
  <p><a href="http://imgur.com/DjI8Udr" rel="nofollow">http://imgur.com/DjI8Udr</a></p>
  
  <p><a href="http://imgur.com/XKQW6DI" rel="nofollow">http://imgur.com/XKQW6DI</a></p>
  
  <p>Entire code: </p>
  
  <pre><code> import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.Timer;
   import java.util.*;
  
  public class Interface extends JFrame implements ActionListener {
  
  private static final long serialVersionUID = 1L;
  
  ImageIcon img_idle = new ImageIcon("img_idle.png");
  ImageIcon img_react = new ImageIcon("img_react.png");
  
  JButton button_1 = new JButton(img_idle);
  JButton start = new JButton("Start");
  
  Random r = new Random();
  
  public Interface() {
      super("Simple Reaction Game");
      setSize(180, 350);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
  
      Container contentArea = getContentPane();
      contentArea.setBackground(Color.white);
  
      FlowLayout flowManager = new FlowLayout();
      contentArea.setLayout(flowManager);
  
      button_1.addActionListener(this);
      start.addActionListener(this);
  
      contentArea.add(button_1);
      contentArea.add(start);
  
      setContentPane(contentArea);
  }
  
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == start) {
          try {
              countDown();
          } catch (InterruptedException e1) {
              e1.printStackTrace();
          }
      }
  }
  
  public void countDown() throws InterruptedException {
      Thread t = new Thread();
      int random = r.nextInt((10000 - 1000) + 1) + 1000;
  
      while(random &gt;= 0){
          t.sleep(1000);
          random -= 1000;
      }
  
      if (random &lt;= 0) {
          button_1.setIcon(img_react);
          repaint();
      }
  
  }
  
  public static void main(String args[]) {
      new Interface();
  }
  
  }
  </code></pre>