在其他线程 Android 中运行的可运行队列

发布于 2024-11-24 13:37:49 字数 954 浏览 0 评论 0原文

该代码将帮助我解释我的问题:

public class TestHandlerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new  Thread(){
        public void run(){
            for (int i=0;i<20;i++){
                handler.post(run);
            }
        }
    }.start();
}

Handler handler=new Handler(){

};

Runnable run = new Runnable(){
    public void run(){
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("TAG", "Message processed");
    }
};
}

这样新线程会发出由主线程中的处理程序提供服务的请愿书。 我需要做的恰恰相反。 UI 线程启动请愿书,并由另一个线程按顺序提供服务(顺序很重要),并且每个请愿书结束时不需要通知 UI 线程。 对此有几点很重要:请愿书有 3 个参数(int、int、boolean),它们在数据库中进行更改,并通过用户与 UI 的交互生成,因此它们不会同时启动 提前致谢

that code will help me explain my problem:

public class TestHandlerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new  Thread(){
        public void run(){
            for (int i=0;i<20;i++){
                handler.post(run);
            }
        }
    }.start();
}

Handler handler=new Handler(){

};

Runnable run = new Runnable(){
    public void run(){
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("TAG", "Message processed");
    }
};
}

That way the new thread makes the petitions which are served by the handler in the main thread.
What i need to do is exactly the opposite. The UI thread launches petitions wich are served sequentially by another thread (the order is important), and the UI thread don't need to be notified when each petition end.
Some points are important for this: The petitions have 3 parameters (int, int, boolean), they make changes in the database and are generated by the interaction of the user with the UI, so they aren't launched simultaneously
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2024-12-01 13:37:49

一种选择是使用它来创建线程:http://developer.android。 com/reference/android/os/HandlerThread.html

这将创建一个具有自己的消息队列和循环的线程。您可以创建一个处理程序来在线程上运行工作,如下所示:

HandlerThread handlerThread = new HandlerThread("My Handler");
handlerThread.start();
Handle myHandler = new Handler(handlerThread.getLooper());

这确实要求线程完成的所有工作都通过处理程序发送消息并在其上调度 Runnables 来完成。

One option is to use this for making your thread: http://developer.android.com/reference/android/os/HandlerThread.html

This will create a thread with its own message queue and loop. You can create a Handler to run work on the thread like so:

HandlerThread handlerThread = new HandlerThread("My Handler");
handlerThread.start();
Handle myHandler = new Handler(handlerThread.getLooper());

This does require that all work done by thread be done so by sending messages and scheduling Runnables on it through Handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文