简体   繁体   中英

Simple java timer not working

I am new to programming and i used following code to run a timer. When this method is called with boolean run = true it is working fine but not stops when boolean run = false . How can i solve this ?

    private void countTime(boolean run){
        new Thread(){
            public void run(){
                int mm = 00;
                int hh = 00;
                while(run){
                    for (int i=1 ;i<61 ;i++){
                        try{
                            int ss = i;
                            Thread.sleep(1000);
                            if (ss == 60){
                                mm += 1;
                                if (mm == 60){
                                }
                            }
                            if (mm == 60){
                                hh += 1;
                                mm = 0;
                            }
                            if (ss < 10){
                                lblClock.setText(Integer.toString(hh) + ":" + 
                                        Integer.toString(mm) + ":" + "0" 
                                        + (Integer.toString(ss))
                                );
                            } else {
                                lblClock.setText(Integer.toString(hh) + ":" + 
                                        Integer.toString(mm) + ":" 
                                        + (Integer.toString(ss))
                                );
                            }
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            }
        }.start();
}

I am guessing that you are starting the timer like countTime(true) and to turn it off you are calling countTime(false) .

You did not notice that the timer is running as separate thread. So calling again you are launching a new thread. Basically each call will launch a new thread, nothing will be updated in any of the previously running threads.

This is a anonymous Thread class which creates a new thread every time you call the countTime(boolean) method.

Can you add " caller " code & code snippet setting run as false?

From current code, countTime starts a new Thread always. That may be the reason. Did not see where " run " was set as false.

Do you want to start new thread always? and How and where are you making run as false?

I think that you need only one thread and you have to set run value " true " or " false " from the caller. Posting caller code will help to fix the problem.

EDIT:

1) Declare one thread separately. Provide setter and getter method for "run" boolean variable.

2) When user click on start button click of "start", start the thread with run as true. If user clicks on start twice, make sure that second thread is not created. If (thread == null) create new thread and ignore the request if thread is not null

3) When user click on stop button & thread is not null, set the run as false.

By calling countTime(false) ,you just start another thread.if you print the time message to the console and click start button twice,you will find there are two timer running. To stop the thread,you might do like this:

Thread thread=new Thread(){
public void run(){
//your timer code here
}};
public synchronized void stop(){
        if(null != thread){
            thread.interrupt();
            thread = null;
            notifyAll();
        }
}
public void start(){
        if(null == thread){
            thread = new Thread(this);
            thread.start();
        }
}

when you want to start the thread,call start() ,and stop() to stop it.

why doing so?when the thread is in the blocked state (by calling Thread.sleep(1000) ) then call thread.interrupt() to let it throw the InterruptedException. I am new to programming too,and had the similar problem,hope that can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM