0

I have an issue with Java thread. Actually I've created a timer but whenever I click the start button more than once the program starts counting fast!!

I want my program to start from zero when I press start the second time with the initial speed.

I tried to fix that but I couldn't find a solution.

Why is this happening, is it something internal to JVM?

here is my code..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;;

public class Timer extends JFrame implements ActionListener
{
    private JPanel panel1,panel2;
    private JButton button;
    private JLabel label;
    private int second = 0;
    private int mint = 0;
    boolean flage = true;

    public Timer()
    {
        super("Timer");
        setSize(300,150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setResizable(false);
        label = new JLabel();
        panel1 = new JPanel(new FlowLayout());
        panel1.add(label);

        button = new JButton("Start");
        panel2 = new JPanel(new FlowLayout());
        panel2.add(button);

        button.addActionListener(this);

        panel1.setBackground(Color.white);
        panel2.setBackground(Color.white);

        add("Center",panel1);
        add("South",panel2);

        setVisible(true);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        Font myFont = new Font("Elephant", Font.BOLD,34);
        g.setFont(myFont);
        g.setColor(Color.black);
        g.drawString(String.format("%02d",mint)+":"+String.format("%02d", second), 90, 80);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button)
        {
            if(flage)
            {

            mythread a = new mythread();
            a.start();
            }
        }
    }

    class mythread extends Thread 
    {
        public void run()
        {
            for(int i=0;i<60;i++)
            {
                for(int j=0;j<60;j++)
                {
                second++;
                if(second==60)
                {
                    second=0;
                    mint++;
                }
                repaint();
                doNothing(1000);
                }
                repaint();
            }
        }

        public void doNothing(int a)
        {
            try
            {
                Thread.sleep(a);
            }
            catch(Exception e)
            {

            }
        }
    }

    public static void main(String[]args)
    {
        new Timer();
    }
}

Thanks

1
  • i just use Thread.stop(); make second = 0; and do Thread.start() again and now it works fine Commented Feb 20, 2014 at 16:14

2 Answers 2

1

You start a new thread each time they press the button.

Each thread is counting down the same variable (but be careful, you can't assume how that will behave due to synchronization issues) so you see the variable count faster and faster as you start more threads.

Just store a reference to the thread when you create it and when they press the button only spawn the thread if it doesn't already exist.

Sign up to request clarification or add additional context in comments.

1 Comment

oh i see, but how could i solve this issue i want to flush the old thread and create a new one..Thanks for ur help :)
0
       when you Enter The thread You set values second=0; min=0;

    public void run()
    {
        second=0;
        mint = 0;
        for(int i=0;i<60;i++)
        {
            for(int j=0;j<60;j++)
            {
            second++;
            if(second==60)
            {
                second=0;
                mint++;
            }
            repaint();
            doNothing(1000);
            }
            repaint();
        }
    }

1 Comment

it's working fine but when i press start button for second time the counter start to speed up more and more..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.