简体   繁体   English

简单的Java计时器不起作用

[英]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 . 当使用boolean run = true调用此方法时,它工作正常,但当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) . 我猜想您正在像countTime(true)这样启动计时器,并且要关闭它,您正在调用countTime(false)

You did not notice that the timer is running as separate thread. 您没有注意到timer正在作为单独的线程运行。 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. 这是一个匿名Thread类,每次您调用countTime(boolean)方法时,该类都会创建一个新线程。

Can you add " caller " code & code snippet setting run as false? 您可以添加“ 主叫方 ”代码和代码段设置为false吗?

From current code, countTime starts a new Thread always. 从当前代码开始, countTime始终会启动一个新线程。 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. 我认为您只需要一个线程,并且必须从调用方设置运行值“ true ”或“ false ”。 Posting caller code will help to fix the problem. 发布呼叫者代码将有助于解决问题。

EDIT: 编辑:

1) Declare one thread separately. 1)分别声明一个线程。 Provide setter and getter method for "run" boolean variable. 为“运行”布尔变量提供setter和getter方法。

2) When user click on start button click of "start", start the thread with run as true. 2)当用户单击开始按钮,单击“开始”时,以run为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 如果(thread == null)如果线程不为null,则创建新线程并忽略请求

3) When user click on stop button & thread is not null, set the run as false. 3)当用户单击停止按钮且线程不为null时,将运行设置为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. 通过调用countTime(false) ,您只需启动另一个线程。如果将时间消息打印到控制台,然后单击两次启动按钮,您将发现有两个计时器正在运行。 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. 当您要启动线程时,请调用start() ,然后调用stop()停止它。

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. 当线程处于阻塞状态时(通过调用Thread.sleep(1000) ),然后调用thread.interrupt()使其抛出InterruptedException。 I am new to programming too,and had the similar problem,hope that can help you 我也是编程的新手,并且遇到了类似的问题,希望可以为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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