什么是Android UiThread(UI线程)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3652560/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:10:46  来源:igfitidea点击:

What is the Android UiThread (UI thread)

androidui-thread

提问by user434885

Can someone explain to me what exactly the UI thread is? On developer.android.com it says about the runOnUiThread function

有人可以向我解释 UI 线程到底是什么吗?在 developer.android.com 上,它说明了 runOnUiThread 函数

public final void runOnUiThread (Runnable action)

Since: API Level 1 Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

public final void runOnUiThread (Runnable action)

自:API 级别 1 在 UI 线程上运行指定的操作。如果当前线程是 UI 线程,则立即执行操作。如果当前线程不是 UI 线程,则将操作发布到 UI 线程的事件队列中。

Does the UI thread mean that this will be run everytime the activity is pushed the the background by some ui activity like incoming call or screen dimming etc.? If not, what exactly does the UI thread include ?

UI 线程是否意味着每次活动被某些 ui 活动(如来电或屏幕变暗等)推送到后台时都会运行?如果没有,UI 线程究竟包括什么?

Thank you

谢谢

回答by plainjimbo

The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread.

UIThread 是应用程序的主要执行线程。这是您的大部分应用程序代码运行的地方。您的所有应用程序组件(活动、服务、内容提供者、广播接收器)都在此线程中创建,对这些组件的任何系统调用都在此线程中执行。

For instance, let's say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like onCreate, onPause, onDestroy, onClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.

例如,假设您的应用程序是单个 Activity 类。然后所有生命周期方法和大部分事件处理代码都在这个 UIThread 中运行。这些是诸如onCreateonPauseonDestroyonClick等方法。此外,这是对 UI 进行所有更新的地方。任何导致 UI 更新或更改的事情都必须在 UI 线程上发生。

For more info on your application's Processes and Threads click here.

有关您的应用程序的进程和线程的更多信息,请单击此处。

When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread. So what happens if this background thread needs to do something that changes the UI? This is what the runOnUiThreadis for. Actually you're supposed to use a Handler (see the link below for more info on this). It provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the runOnUiThread method.

当您显式生成一个新线程以在后台执行工作时,此代码不会在 UIThread 上运行。那么如果这个后台线程需要做一些改变 UI 的事情会发生什么?这就是runOnUiThread它的用途。实际上,您应该使用处理程序(有关更多信息,请参见下面的链接)。它为这些后台线程提供了执行可以修改 UI 的代码的能力。他们通过将 UI 修改代码放入 Runnable 对象并将其传递给 runOnUiThread 方法来实现这一点。

For more info on spawning worker threads and updating the UI from them click here

有关生成工作线程和从它们更新 UI 的更多信息,请单击此处

I personally only use the runOnUiThreadmethod in my Instrumentation Tests. Since the test code does not execute in the UIThread, you need to use this method to run code that modifies the UI. So, I use it to inject click and key events into my application. I can then check the state of the application to make sure the correct things happened.

我个人只runOnUiThread在我的仪器测试中使用该方法。由于测试代码不在 UIThread 中执行,因此需要使用此方法运行修改 UI 的代码。所以,我用它来将点击和按键事件注入到我的应用程序中。然后我可以检查应用程序的状态以确保发生了正确的事情。

For more info on testing and running code on the UIThread click here

有关在 UIThread 上测试和运行代码的更多信息,请单击此处

回答by Martin Pabst

If you execute blocking code (e.g. a Http-Request) in a separate Thread, consider using AsyncTask. Its doInBackground-Method runs on a separate Thread. AsyncTaskprovides you with methods onProgressUpdateand onPostExecutewhich are guaranteed to run on the UI thread.

如果您在单独的线程中执行阻塞代码(例如 Http-Request),请考虑使用AsyncTask。它的doInBackground-Method 在单独的线程上运行。AsyncTask为您提供了方法onProgressUpdateonPostExecute保证在UI线程上运行

If you need GUI-progress updates (e.g. via a progressbar) call publishProgressinside doInBackground. This leads to a subsequent call of onPublishProgresswhich is also guaranteed to run on the UI thread.

如果您需要 GUI 进度更新(例如通过进度条),请publishProgressdoInBackground. 这导致随后的调用onPublishProgress保证在 UI 线程上运行

onPostExecuteis automatically called after doInBackgroundreturns.

onPostExecutedoInBackground返回后自动调用。

回答by the100rabh

All UI drawings etc. happen in a separate thread. Its called the UIThread. If you want to make any change to UI u must use make sure it happens in UIThread's context. Easiest way of doing it is to make use of runOnUiThread

所有 UI 绘图等都发生在一个单独的线程中。它被称为 UIThread。如果您想对 UI 进行任何更改,您必须确保它发生在 UIThread 的上下文中。最简单的方法是利用runOnUiThread