最佳的定期执行操作的最佳方法[正在运行应用程序时]-处理程序? [英] Best way to perform an action periodically [while an app is running] - Handler?

查看:74
本文介绍了最佳的定期执行操作的最佳方法[正在运行应用程序时]-处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定期执行操作.我想在3秒后创建一个类的新实例.最好通过使用处理程序或线程来实现这一点吗?有没有一种我可以尝试的更简单,更真实的方法?我确实不擅长使用线程-我想学习,但是更重要的是,在担心良好的编程习惯之前,请使其正常运行.

I'm trying to perform an action periodically. I want to create a new instance of a class after, say, ever 3 seconds. Would it be best to implement this by using a Handler or a Thread? Is there an easier, sort of dopey way I could try? I'm really not good at using threads - I want to learn, but it is more important that I get this to function before worrying about good programming practices.

    new Thread(){
        public void run(){
            //makes sure the player still has 3 lives left
            while(game == false){
                uiCallback.sendEmptyMessage(0);
                try {
                    Thread.sleep(2000); // wait two seconds before drawing the next flower
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //sleep for 2 seconds
            }
        }
    }.start();

推荐答案

我正在我的android应用中执行类似的操作;我每10秒钟更新一次界面中的一些数据.有很多方法可以做到这一点,但是我选择使用Handler,因为它实现起来非常简单:

I'm doing something similar in my android app; I update some data in my interface every 10 seconds. There are many ways to do this, but I chose to use a Handler because it's very simple to implement:

Thread timer = new Thread() {
    public void run () {
        for (;;) {
            // do stuff in a separate thread
            uiCallback.sendEmptyMessage(0);
            Thread.sleep(3000);    // sleep for 3 seconds
        }
    }
});
timer.start();
...
private Handler uiCallback = new Handler () {
    public void handleMessage (Message msg) {
        // do stuff with UI
    }
};

您可能知道,您不能在UI线程中运行像这样的定期函数,因为它将阻塞UI.这样会创建一个新的Thread,该Thread完成后会向UI发送消息,因此您可以使用定期函数执行的新结果来更新UI.

As you may know, you cannot run periodic functions like this in the UI thread, because it will block the UI. This creates a new Thread that sends a message to the UI when it is done, so you can update your UI with the new results of whatever your periodic function does.

如果不需要使用此定期函数的结果更新UI,则可以简单地忽略我的代码示例的后半部分,而只需生成一个新的Thread即可,如图所示.但是要当心:如果您要修改此新Thread和UI共享的变量,那么如果不同步,将会遇到问题.通常,线程化不是您要忽略良好编程习惯"的地方,因为这样会产生奇怪的,不可预测的错误,并且会诅咒您的程序.

If you do not need to update the UI with the results of this periodic function, you can simply ignore the second half of my code example, and just spawn a new Thread as shown. Beware, however: if you are modifying variables shared by this new Thread and the UI, you are going to run into problems if you don't synchronize. In general, threading is not an area where you want to ignore "good programming practices" because you'll get strange, non-predictable errors and you'll be cursing your program.

-tjw

这篇关于最佳的定期执行操作的最佳方法[正在运行应用程序时]-处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆