处理器 - 定期执行的操作的最佳方式?处理器、操作、方式

2023-09-04 06:47:09 作者:就愛裝逼╮

我想定期执行的操作。我想以后创建一个类的新实例,说,以往3秒。难道是最好用处理程序或线程来实现这一点?是否有更简单,有点迟钝的方式,我可以试试吗?我真的不擅长使用线程 - 我想学习,但更重要的是,我得到这个担心良好的编程习惯才起作用。

 新的Thread(){
        公共无效的run(){
            //确保玩家仍留下3个生命
            而(游戏== FALSE){
                uiCallback.sendEmptyMessage(0);
                尝试 {
                    视频下载(2000); //等待两秒钟绘制的下花前,
                }赶上(InterruptedException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                } //睡眠2秒
            }
        }
    }。开始();
 

解决方案

我做我的Andr​​oid应用程序类似的东西;我更新我的界面每10秒的一些数据。有很多方法可以做到这一点,但我选择使用处理程序,因为它很容易实现:

 发定时器=新的Thread(){
    公共无效的run(){
        对于 (;;) {
            //做的东西在一个单独的线程
            uiCallback.sendEmptyMessage(0);
            视频下载(3000); //睡眠3秒
        }
    }
});
timer.start();
...
私人处理程序uiCallback =新的处理程序(){
    公共无效的handleMessage(信息MSG){
        //做的东西与UI
    }
};
 

正如你可能知道,你不能在UI线程中运行周期函数这样的,因为它会阻止用户界面。这将创建一个新的将消息发送到用户界面,当它完成,所以你可以用任何你的周期函数做的新成果更新你的用户界面。

CPU计算机性能的操作方法,电脑CPU性能测试最佳方法分享 赶紧学一下

如果您不需要更新与此周期函数的结果的用户界面,你可以简单地忽略我的code为例下半年,只是产生一个新的,如图所示。但是要小心,:如果要修改这个新的和用户界面的共享变量,你会遇到问题,如果你不同步。一般情况下,线程是不是要忽略良好的编程习惯,因为你会得到奇怪的,非predictable错误的区域,你会骂你的程序。

-tjw

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();

解决方案

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
    }
};

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.

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