I want to create a Java program that allows me to repeat a sequence of - 5, 4, 3, 2, 1, 0 FIRE - the number of times i want.
I started by searching how can i create a countdown, and i use a timer schedule so i can set the JTextFields text every 1 second ( to produce the effect of a normal countdown). But the problem is that when i want to repeat, lest say, 2 times this countdown, my java code is starting both countdowns at the same time and not waiting for the first one to finish. My main goal is to click a button and start a certain number of countdowns. The number of countdowns to produce is inputed in the enabled JTextField.

I leave my entire code here:

```
    import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;

import javax.swing.*;

public class Counter {

    public static class MyThread implements Runnable {
        private JTextField tx1;
        private JTextField tx2;
        CountDownLatch latch;

        int count;

        public MyThread (JTextField tx1,JTextField tx2,CountDownLatch latch) {
            this.tx1=tx1;
            this.tx2=tx2;
            this.latch = latch;
          }

        public void run() {
                 tx1.setText("");
                    tx2.setText("");
                    count=5;
                    Timer timer = new Timer();
                    timer.scheduleAtFixedRate(new TimerTask() {
                        int counter=0;
                          public void run() {
                             tx1.setText("");
                            tx1.setText(Integer.toString(count));
                            System.out.println("!");
                            count--;
                            counter++;
                            if (counter==6){
                                tx2.setText("FIRE");
                                this.cancel();
                            }
                          }
                        }, 1000,1000);
                        latch.countDown();

          } 
    }

public static void main(String[] args) {
    JFrame frame = new JFrame ("Counter");
    JPanel panel=new JPanel ();
    panel.setLayout(new GridLayout(3,2));
    JButton bt1=new JButton("Start");
    JButton bt2=new JButton("Reset");
    JLabel lb1=new JLabel ("Puffs");
    final JTextField tx1= new JTextField(5);
    final JTextField tx2= new JTextField(10);
    final JTextField tx3= new JTextField(5);
    tx1.setEnabled(false);
    tx2.setEnabled(false);
    panel.add(tx1);
    panel.add(tx2);
    panel.add(lb1);
    panel.add(tx3);
    panel.add(bt1);
    panel.add(bt2);
    frame.add(panel);

    CountDownLatch latch = new CountDownLatch(2);

    new Thread(new MyThread(tx1, tx2,latch)).start();
    new Thread(new MyThread(tx1, tx2,latch)).start();
    try {
        latch.await();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    bt1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
//          int tr=Integer.parseInt(tx3.getText());

        }   
    });

    bt2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {

            }

    });

    ////
    GraphicsConfiguration gc = frame.getGraphicsConfiguration();
    Rectangle bounds = gc.getBounds();

    Dimension size = frame.getPreferredSize();
    frame.setLocation((int) ((bounds.width / 2) - (size.getWidth() / 2)), 
            (int) ((bounds.height / 2) - (size.getHeight() / 2)));

    frame.setVisible(true);
    frame.pack();
     }
  }

```

Sorry about bad formatting and bad writting.
Much appreciated by your help!

Edited CODE

```
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;

import javax.swing.*;

public class Counter {

    public static class MyThread implements Runnable {
        private JTextField tx1;
        private JTextField tx2;

        int count;

        public MyThread (JTextField tx1,JTextField tx2) {
            this.tx1=tx1;
            this.tx2=tx2;
          }

        public void run() {
                 tx1.setText("");
                    tx2.setText("");
                    count=5;
                    Timer timer = new Timer();
                    timer.scheduleAtFixedRate(new TimerTask() {
                        int counter=0;
                          public void run() {
                             tx1.setText("");
                            tx1.setText(Integer.toString(count));
                            System.out.println("!");
                            count--;
                            counter++;
                            if (counter==6){
                                tx2.setText("FIRE");
                                this.cancel();
                            }
                          }
                        }, 1000,1000);

          } 
    }

public static void main(String[] args) {
    JFrame frame = new JFrame ("Counter");
    JPanel panel=new JPanel ();
    panel.setLayout(new GridLayout(3,2));
    JButton bt1=new JButton("Start");
    JButton bt2=new JButton("Reset");
    JLabel lb1=new JLabel ("Puffs");
    final JTextField tx1= new JTextField(5);
    final JTextField tx2= new JTextField(10);
    final JTextField tx3= new JTextField(5);
    tx1.setEnabled(false);
    tx2.setEnabled(false);
    panel.add(tx1);
    panel.add(tx2);
    panel.add(lb1);
    panel.add(tx3);
    panel.add(bt1);
    panel.add(bt2);
    frame.add(panel);

    bt1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
//          int tr=Integer.parseInt(tx3.getText());
            Thread t1 = new Thread(new MyThread(tx1, tx2));
            Thread t2 = new Thread(new MyThread(tx1, tx2));
            t1.start();
            try {
                t1.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            t2.start();
            try {
                t2.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    });

    bt2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            }

    });

    ////
    GraphicsConfiguration gc = frame.getGraphicsConfiguration();
    Rectangle bounds = gc.getBounds();
    Dimension size = frame.getPreferredSize();
    frame.setLocation((int) ((bounds.width / 2) - (size.getWidth() / 2)), 
            (int) ((bounds.height / 2) - (size.getHeight() / 2)));
    frame.setVisible(true);
    frame.pack();
     }
  }

```