android - 为什么使用单线程模型作为主线程来更新 UI?

标签 android ios qt frameworks ui-thread

Qt 文档说,

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread.

Android 文档说,

Like activities and the other components, services run in the main thread of the application process

和 iOS,

It is strongly recommended not to update UI controls etc from a background thread (e.g. a timer, comms etc). This can be the cause of crashes which are sometimes very hard to identify. Instead use these to force code to be executed on the UI thread (which is always the “main” thread).

为什么他们使用单线程模型来更新 UI?

最佳答案

简短的回答是,这是确保显示不损坏的唯一合理方法。

长话短说,允许多个线程更新 UI 会导致死锁、竞争条件和各种麻烦。这是 Java 的 AWT(以及其他 UI 系统)允许多个线程访问 UI 的惨痛教训。参见,例如,Multithreaded toolkits: A failed dream? .该帖子(通过无效链接)指的是Why Threads Are A Bad IdeaThreadaches .

关于android - 为什么使用单线程模型作为主线程来更新 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772658/

相关文章:

android - 以编程方式将项目添加到 ScrollView 顶部,但不应滚动到顶部

java - 显示每帧变化的文本(如乐谱)

android - 如何从 ListView 中删除 Firebase Android 中的特定节点

ios - 为什么 iOS App 在空闲时导致 Xcode 内存泄漏?

c++ - 在 QtCreator 3.6 中运行两个应用程序

qt - Yocto:如何删除图层而不重建所有图层

java - 如何从选定的联系人中提取电话号码?

ios - 使用 AFNetworking 向请求添加参数

ios - 使用新的苹果开发面板生成推送通知 ssl 证书和配置?

c++ - 显示 QMenu 以响应在 QLabel 中右键单击,可能吗?