Android 主线程和 UI 线程一样吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3261370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is main thread the same as UI thread?
提问by user256239
The Android doc says "Like activities and the other components, services run in the main thread of the application process."
Android 文档说“与活动和其他组件一样,服务在应用程序进程的主线程中运行。”
Is the main thread here the same thing as UI thread?
这里的主线程和 UI 线程是一样的吗?
回答by Andy Zhang
Looks like it. Quoted from http://android-developers.blogspot.com/2009/05/painless-threading.html: "When an application is launched, the system creates a thread called "main" for the application. The main thread, also called the UI thread...", Official APIdocument.
看起来像。引自http://android-developers.blogspot.com/2009/05/painless-threading.html:“当应用程序启动时,系统会为该应用程序创建一个名为“main”的线程。主线程,也称为UI 线程...”,官方API文档。
回答by Kartihkraj Duraisamy
UI Threadand Main Threadare same only in Android.
UI 线程和主线程仅在Android中相同。
The Main thread, that is responsible for handling the UI eventslike Draw, Listen and receive the UI events.
在主线程,也就是负责处理UI事件一样抽奖,听和接收UI事件。
Ans also it is responsible for interact with running componentsof the UI toolkitfor the corresponding application that belongs to.
Ans 还负责与所属的相应应用程序的UI 工具包的运行组件进行交互。
When an User event occurs in the application, the Main thread*
当应用程序中发生用户事件时,主线程*
need to add the event in the queue -> intimate about the event to appropriate View -> change the state of the view -> redraw the view according to the state changes -> waiting for the response for the particular event action -> after intimated and event action completed need to delete the event in the queue.
需要在队列中添加事件 -> 将事件通知到合适的 View -> 改变视图的状态 -> 根据状态变化重绘视图 -> 等待特定事件动作的响应 -> 通知之后和事件动作完成需要删除队列中的事件。
*
*
The above every actions are handled by the Main thread(Not only the above operation, it is a one of the operation handled by the UI Thread), So if our application fails to respond the event about 5 secondsandroid will shows the error "not responding".
上面的每一个动作都是由主线程处理的(不仅是上面的操作,它也是UI 线程处理的操作之一),所以如果我们的应用程序在大约5 秒内未能响应事件,android 将显示错误“not回应”。
So only it is widely suggested to do the light processes in the UI thread.
所以只广泛建议在UI 线程中做轻量级的进程。
Hope this answer is somewhat detail and helpful to the new android bees like me. I just shared what i learned about UI Thread. If i went wrong in anywhere please don't hesitate to recorrect me.
希望这个答案有点详细,对像我这样的新 android 蜜蜂有帮助。我只是分享了我对 UI Thread 的了解。如果我在任何地方出错,请随时纠正我。
回答by CommonsWare
The "main application thread" is sometimes called the "UI thread".
“主应用程序线程”有时也称为“UI 线程”。
回答by Leo Droidcoder
Basically Main Thread is Ui Thread.
However sometimes they can be different treads!
It is possible for system apps with multiple views on different threads.
Also if you use support annotations note that both @MainThread
and @UiThread
are available at the same time.
Here with the first one you annotate methods associated with the App life cycle and with the second one methods that are in charge of view hierarchy.
https://developer.android.com/studio/write/annotations.html
基本上主线程是 Ui 线程。
然而,有时它们可能是不同的胎面!
对于在不同线程上具有多个视图的系统应用程序是可能的。
此外,如果您使用支持注释,请注意@MainThread
和@UiThread
可同时使用。
在这里,第一个是注释与 App 生命周期相关的方法,第二个是负责视图层次结构的方法。
https://developer.android.com/studio/write/annotations.html
回答by Rajdeep Dua
Every Activity has its own UI thread. As soon as the VM boots up, System Server is started by the Zygote. All other services like Activity Manager Service are started in new threads by the System Server.
每个 Activity 都有自己的 UI 线程。一旦 VM 启动,Zygote 就会启动 System Server。系统服务器在新线程中启动所有其他服务,如活动管理器服务。
回答by Nguyen Minh Binh
Yes. main thread is UI thread.
是的。主线程是 UI 线程。
See this tutorialfor full details about background processing in android
有关android 中后台处理的完整详细信息,请参阅本教程