Android Java runOnUiThread()

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

Android Java runOnUiThread()

javaandroid

提问by Rasmus

I have been messing around a bit with the runOnUiThread method. And if I simply make a method in my activity:

我一直在弄乱 runOnUiThread 方法。如果我只是在我的活动中创建一个方法:

public void Test()
{
    runOnUiThread(new Runnable() 
    {
        public void run() 
        {
            Log.v("mainActivity", "test");
        }
    });     
}

I noticed that this runnable only runs once. However, this is not a problem. What I was wondering is if I have completely missed something and it does something in the background that would cause a frame rate drop when I have executed the method a couple times.

我注意到这个 runnable 只运行一次。然而,这不是问题。我想知道的是,我是否完全错过了某些东西,并且它在后台执行了一些操作,当我执行该方法几次时会导致帧速率下降。

采纳答案by Austin B

This is the full body from Activity.runOnUiThread(Runnable):

这是Activity.runOnUiThread(Runnable)的完整内容:

public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else {
        action.run();
    }
}

The method body is still executed in your background thread, and mHandler of class android.os.Handlerimplements an internal queue for Runnables posted to it, so unless you're doing blocking work in the Runnable (which is a bigno-no on the UI Thread) or calling this method upwards of a thousand times in a short period, you should not see any difference.

该方法的主体是在你的后台线程仍然执行,类mHandler android.os.Handler实现了的Runnable内部队列中的发布到它,所以,除非你正在做的工作,堵在了Runnable(这是一个很大的不不上UI 线程)或在短时间内调用此方法上千次,您应该看不到任何区别。

Now, if you were calling Handler.postAtFrontOfQueue(Runnable), then there'd be an issue, because your Runnable is essentially "cutting in line". In this case, that would likely cause a stutter, because your Runnable is being executed instead of any UI updates that needed to take place (like scrolling).

现在,如果您正在调用Handler.postAtFrontOfQueue(Runnable),那么就会出现问题,因为您的 Runnable 本质上是“排队”。在这种情况下,这可能会导致卡顿,因为您的 Runnable 正在执行,而不是任何需要发生的 UI 更新(如滚动)。

Note that you only needto run UI updates on the UI thread, like calling any methods on a View (thus the name "UI Thread" and why this method exists) or any operation where the documentation explicitly states that it needs to be run on the UI thread. Otherwise, if you're already on a background thread, there's no real reason to leave it.

请注意,您只需要在 UI 线程上运行 UI 更新,例如调用视图上的任何方法(因此名称为“UI 线程”以及此方法存在的原因)或文档明确指出需要在其上运行的任何操作用户界面线程。否则,如果您已经在后台线程中,就没有真正的理由离开它。

回答by GreyBeardedGeek

It's unlikely that it would cause any significant interruption to your UI process, but there's really no point in running it on the UI thread.

它不太可能对您的 UI 进程造成任何重大中断,但在 UI 线程上运行它确实没有意义。

If you are doing any significant amount of work, you should make sure that you do not do it on the UI thread.

如果您正在做大量的工作,您应该确保不要在 UI 线程上进行。