Runnables会阻止UI线程吗? [英] Will Runnables block the UI thread?
问题描述
我试图了解UI线程的事件队列如何工作.我正在尝试运行一个可以分为许多部分的代码,但是不幸的是,它必须在UI线程上运行.因此,为了不阻塞UI线程并接收ANR,我想知道是否可以在许多Runnable
对象中破坏该代码并使用另一个线程中的runOnUiThread
来运行它们.
I am trying to understand how UI thread's event queue works. I'm trying to run a code that can be broken into many parts but unfortunately it must run on the UI thread. So, in order to not block the UI thread and receive a ANR I was wondering if I can break that code in many Runnable
objects and run them using runOnUiThread
from another thread.
我的问题是,这会阻塞UI线程吗?例如,如果我有一段代码肯定在5秒钟内运行并且将这段代码分解为1000个Runnable
对象,并将它们添加到UI线程的事件队列中,其他事件是否会得到处理通过它们之间的UI线程?
My question is, will this block the UI thread? If, for example, I have a piece of code that definitely runs in over 5 seconds and I break this code into, let's say 1000 Runnable
objects, and add them to the event queue of the UI thread, will other events get processed by the UI thread between them?
编辑:我认为我发现了一种更好的表达自己的方式,以防上述解释造成混淆.
I think I found a better way to express myself in case the above explanation is confusing.
- 1000个
Runnable
对象只是一个示例,在实际代码中,我最多希望有10个. - 基本上,我需要10个
Runnable
对象,每个对象在UI线程上初始化一个广告网络.我希望这些Runnable
对象一个接一个地运行,而不是并行运行.另外,我希望UI线程能够在运行这些对象之间处理其他事件,以便在运行所有10个run
方法都需要5秒钟以上的情况下,我不会得到ANR.
- The 1000
Runnable
objects was just an example, in actual code I want to have at most 10. - Basically, I want 10
Runnable
objects, each one initialising an Ad network on the UI thread. I want theseRunnable
objects to run one after another, not in parallel. Also, I want the UI thread to be able to process other events between running these objects, so that I don't get an ANR in case running all 10run
methods will take more than 5 seconds.
注意::我不知道为什么必须在UI线程上完成广告网络的初始化,但是必须初始化,否则应用程序将崩溃.它还在一些网络的sdks文档中指出,初始化必须在UI线程上进行.这就是为什么我需要在UI线程上一个接一个地运行它们,而不能在后台并行运行它们的原因.
另外,该应用程序实际上是OpenGl游戏,因此调用Runnable
对象的操作将是从GL线程而不是主线程进行的,因此将它们添加到事件队列中,并且不会立即执行.
NOTE: I don't know why initialising Ad networks must be done on the UI thread, but it must, otherwise the app crashes. It also states in some of the networks' sdks' documentation that initialisation must happen on the UI thread. This is why I need to run them one after another on UI thread and I can't run them in parallel in the background.
Also, the app is actually a OpenGl game, so calls to running the Runnable
objects will be made from a GL thread, not the main thread, so they will be added to the event queue, and not executed immediately.
推荐答案
是.在UI线程上执行的Runnable
将阻塞主线程.
Yes. Runnable
executing on UI thread will block main thread.
检查以下方法对您是否有用.
Check if below approach is useful for you.
- 使用
处理程序: //main中的//developer.android.com/reference/android/os/Looper.html"rel =" nofollow noreferrer> Looper : - 使用Looper为主线程创建处理程序:
responseHandler
并覆盖handleMessage
方法post
requestHandler
Runnable
任务- 在
上调用Runnable
任务中,在responseHandler
sendMessage
- 此
sendMessage
结果调用responseHandler
中的handleMessage
.- 从
Message
获取属性并进行处理,更新UI - 使用Looper为主线程创建处理程序:
- Create a Handler with Looper from Main :
requestHandler
- Create a Handler with Looper for main thread :
responseHandler
and overridehandleMessage
method post
aRunnable
task onrequestHandler
- Inside
Runnable
task, callsendMessage
onresponseHandler
- This
sendMessage
result invocation ofhandleMessage
inresponseHandler
. - Get attributes from the
Message
and process it, update UI
示例代码:
/* Handler */
Handler requestHandler = new Handler(Looper.getMainLooper());
final Handler responseHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
//txtView.setText((String) msg.obj);
Toast.makeText(MainActivity.this,
"Adwork task is completed:"+(String)msg.obj,
Toast.LENGTH_LONG)
.show();
}
};
for ( int i=0; i<10; i++) {
// Start Adwork task
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
/* Your business logic goes here */
// Send some result after computation
String text = "" + (++rId);
Message msg = new Message();
msg.obj = text.toString();
responseHandler.sendMessage(msg);
System.out.println(text.toString());
} catch (Exception err) {
err.printStackTrace();
}
}
};
requestHandler.post(myRunnable);
}
有用的文章:
android-looper-handler-handlerthread-我
这篇关于Runnables会阻止UI线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!