You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

runOnUiThread请求部分服务器数据后延迟30秒执行问题排查

问题描述

在onResume方法中启动线程获取服务器数据,完成后调用runOnUiThread执行UI更新逻辑。请求部分服务器(如cnn.com)时,runOnUiThread会延迟约30秒才执行,但请求stackoverflow.com时无此延迟问题。

完整代码(onResume方法)

@Override
public void onResume() {

    super.onResume();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.e("TESTUI_1", "");

                String urlname = "https://stackoverflow.com";//runonuithread starts immediately
                //String urlname = "https://cnn.com";//runonuithread waits 30 seconds
                URL url = new URL(urlname);
                Log.e("TESTUI_1", urlname);
                HttpURLConnection connectionCache = (HttpURLConnection) url.openConnection();
                connectionCache.setDoOutput(true);

                connectionCache.setRequestProperty("Content-Type", "application/json");//MAn680
                connectionCache.setRequestMethod("POST");//MAn680
                connectionCache.setRequestProperty("Connection", "close");//MAn1140
                connectionCache.setConnectTimeout(10000);
                connectionCache.setReadTimeout(10000);
                connectionCache.setDoInput(true);//3.9.13
                connectionCache.setUseCaches(false); //3.9.13

                int status = connectionCache.getResponseCode();//3.9.12
                Log.e("TESTUI_status", ""+status);
                InputStream is;

                if (status >= 400) {
                    is = connectionCache.getErrorStream();
                } else {
                    is = connectionCache.getInputStream();
                }
            } catch (Exception e) {
                Log.e("TESTUI_error", e.getLocalizedMessage());
            }
            long when = new Date().getTime();
            Log.e("TESTUI_2", ""+when);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    long delta = new Date().getTime() - when;
                    Log.e("TESTUI_3", ""+delta);
                }
            });
        }
    }).start();
}

请求cnn.com时的Logcat输出

TESTUI_1 E

TESTUI_1 E https://cnn.com

TESTUI_status E 200

TESTUI_2 E 1693556722917

TESTUI_3 E 29643

请求stackoverflow.com时的Logcat输出

TESTUI_1 E

TESTUI_1 E https://stackoverflow.com

TESTUI_status E 200

TESTUI_2 E 1693556962546

TESTUI_3 E 230

可在Logcat中搜索的排查关键词

  • Looper/MessageQueue:查看UI线程消息队列是否被阻塞,是否有大量待处理消息导致runOnUiThread的Runnable排队延迟
  • HttpURLConnection/close/disconnect:检查请求cnn.com时连接是否及时关闭,是否存在资源未释放导致的线程阻塞
  • Thread/WAITING/BLOCKED:查看后台线程在网络请求完成后是否进入异常状态,无法及时触发UI线程调用
  • ANR/Application Not Responding:排查是否存在隐性ANR导致UI线程卡顿
  • InputStream/read/skip:检查是否在获取输入流后未完整读取或关闭流,导致连接资源未释放
  • 网络框架底层日志:搜索HttpURLConnection相关系统日志,查看请求完成后是否有额外耗时操作

内容的提问来源于stack exchange,提问作者michaelsmith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:05:24