在主线程处理程序的 post(Runnable r) 中与在主线程中运行有什么区别

发布于 2024-10-05 12:41:29 字数 202 浏览 0 评论 0原文

//case 1:post to main thread's handler
mHandler.post(new Runnable(){
    public void run(){
      dosomething();  //update UI
    });

//case 2:run in main thread
dosomething();
//case 1:post to main thread's handler
mHandler.post(new Runnable(){
    public void run(){
      dosomething();  //update UI
    });

//case 2:run in main thread
dosomething();

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

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

发布评论

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

评论(2

烟柳画桥 2024-10-12 12:41:29

即使您在处理程序的线程上,情况 1 也会排队,情况 2 将立即执行。

Even if you are on the handler's thread, case 1 will be queued and case 2 will be executed immediately.

乖乖兔^ω^ 2024-10-12 12:41:29

好吧,如果你在主线程中,那么你就在主线程中,所以没关系。

但通常情况下,您处于工作线程中并且想做一些只能在主线程中完成的事情(例如更新 UI)。在这些情况下,您需要执行一些特殊操作才能在主线程上运行代码 - 您发布的处理程序代码就是一个示例。

Well, if you're in the main thread, then you're in the main thread, so that's fine.

But pretty often, you're in a worker thread and want to do something that can only be done in the main thread (like updating the UI). In those cases, you need to do something special to run your code on the main thread - the handler code you posted would be one example.

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