简体   繁体   English

当离开主线程时,如何才能使一些代码尽快在主线程上运行?

[英]When off the main thread, how can I get some code to run on the main thread as quickly as possible?

I have an user interface that is partly web based (WebView). 我有一个部分基于Web的用户界面(WebView)。 It is connected to the Android UI through a Javascript Interface . 它通过Javascript接口连接到Android UI。 When you tap on an element in the WebView, javascript calls up to Android and Android receives the call on the javascript/web thread. 当您点击WebView中的某个元素时,JavaScript调用将达到Android,Android将在javascript / web线程上收到该调用。 Not the UI (main) thread. 不是UI(主)线程。

It arrives in Android in 1 or less milliseconds. 它会在1毫秒或更短的时间内到达Android。 No problem there. 没问题。 However, because I want to now change the UI, I have to switch over to the UI thread. 但是,由于我现在想更改UI,因此必须切换到UI线程。 (Android throws an exception if you modify the UI from off the main thread). (如果您从主线程修改UI,则Android会引发异常)。 I am currently using a Handler on the UI Thread and calling post() . 我目前在UI线程上使用Handler并调用post()

This code (a Runnable) is then called anywhere between 120 and 300 ms later. 然后在120到300毫秒之间的任何地方调用此代码(可运行)。 This is a very noticeable lag for the UI to change from the user's touch. 对于UI而言,从用户的触摸改变是非常明显的滞后。

Is there any way to get some code to run on the UI thread faster? 有什么方法可以使一些代码更快地在UI线程上运行? Here is some example code: 这是一些示例代码:

An interface class: 接口类:

public class JSInterface {

    public void test() {
        // Arrives here in 1ms after calling AndroidInterface.test(). Arrives n the web thread.

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // Arrives here 100ms to 300ms after calling AndroidInterface.test(). Arrives on the main (UI) thread.
            }

        });

    }

}

Added to a webview like this: 像这样添加到网络视图中:

webview.addJavascriptInterface(new JSInterface(), "AndroidInterface");

Called in javascript like this: 在javascript中这样调用:

AndroidInterface.test();

Thanks! 谢谢!

Is there any way to get some code to run on the UI thread faster? 有什么方法可以使一些代码更快地在UI线程上运行?

runOnUiThread() and kin put a message on a message queue that the main application thread works off of. runOnUiThread()和kin将消息放在主应用程序线程runOnUiThread()的消息队列中。 Much of the time, the main application thread's job is to pull a message off the queue and process it. 在大多数情况下,主应用程序线程的工作是将消息拉出队列并进行处理。 However, the main application thread is responsible for calling most of your callbacks as well. 但是,主应用程序线程也负责调用大多数回调。

If you are seeing "120 and 300 ms" delays, that means one of two not-mutually-exclusive things: 如果您看到“ 120和300 ms”的延迟,则表示这是两个互斥的事物之一:

  1. The queue has quite the backlog 队列有很多积压

  2. The main application thread is busy executing some other code 主应用程序线程正在忙于执行其他一些代码

The relationship between WebView , the queue, and the main application thread is rather mysterious, compared to normal widgets, because WebView isn't a classic widget rendered in Java code. 与普通的小部件相比, WebView ,队列和主应用程序线程之间的关系相当神秘,因为WebView并不是用Java代码呈现的经典小部件。 Hence, I have no idea if there is something that is occurring in your Web content that might explain this, or if this is fairly normal for WebView . 因此,我不知道您的Web内容中是否正在发生某些事情可以解释这一点,或者对于WebView这是否很正常。 You might try an experiment with simpler Web content and see whether you get similar delays, or if the delays are somehow more tied to the specific Web content that you are rendering. 您可以尝试使用更简单的Web内容进行实验,看看是否收到类似的延迟,或者延迟是否与您正在呈现的特定Web内容更多地相关。

Push come to shove, use Handler and postAtFrontOfQueue() . 推来推去,使用HandlerpostAtFrontOfQueue() As the JavaDocs for that method note, though, this is dangerous. 但是,正如该方法的JavaDocs所述 ,这很危险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM