Kotlin:runBlocking导致App应用出现ANR问题实例

runBlocking简介

runBlocking 是常规函数;

runBlocking 方法会阻塞当前线程来等待;

runBlocking 的主线程会一直 阻塞 直到 runBlocking 内部的协程执行完毕。

runBlocking导致App应用出现ANR问题实例的效果

点击页面上的 刷新按钮 调用 refreshByrunBlocking方法,此方法里模拟了等待30秒耗时操作,当点击 刷新按钮 等待3秒左右,点击 详情按钮 ,页面出现ANR弹框如下图所示

页面布局activity_test_anr_by_runblocking.xml代码

bash 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_refresh"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="60dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:onClick="onClick"
        android:text="刷新"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_detail"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:onClick="onClick"
        android:text="详情"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@+id/btn_refresh"
        app:layout_constraintRight_toRightOf="@+id/btn_refresh"
        app:layout_constraintTop_toBottomOf="@+id/btn_refresh" />

    <TextView
        android:id="@+id/tv_detail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:onClick="onClick"
        android:textColor="@color/black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="@+id/btn_detail"
        app:layout_constraintRight_toRightOf="@+id/btn_detail"
        app:layout_constraintTop_toBottomOf="@+id/btn_detail"
        tools:text="用户信息" />


</androidx.constraintlayout.widget.ConstraintLayout>

实体类:PsersonBean.kt代码

bash 复制代码
data class PsersonBean(val name: String, var moblie: String? = null)//至少有一个构造函数

TestANRByRunBlockingActivity.kt代码

bash 复制代码
package example.demo.kotlin.activity

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import example.demo.kotlin.R
import example.demo.kotlin.bean.PsersonBean
import example.demo.kotlin.utils.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking


class TestANRByRunBlockingActivity : Activity() {
    private lateinit var tv_detail: TextView
    private var psersonBean: PsersonBean

    init {
        psersonBean = PsersonBean("测试用户01")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_anr_by_runblocking)
        initView()
    }

    override fun onStart() {
        super.onStart()
        showData()
    }

    fun initView() {
        tv_detail = findViewById(R.id.tv_detail)
    }

    fun showData() {
        //使用lateinit var 延时初始化,这里安全起见,判断是否 isInitialized
        if (::tv_detail.isInitialized) {
            tv_detail.setText("$psersonBean")//注意,因为PsersonBean是 data class 类型 不需要重新toString 函数
        }
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_refresh -> refreshByrunBlocking2()
            R.id.btn_detail -> detail()
        }
    }

    /**
     * 出现了ANR问题
     */
    fun refreshByrunBlocking() {

        runBlocking(context = Dispatchers.IO) {
            LogUtil.i("开始执行 刷新 耗时操作了")
            delay(30000)//假设30秒,可以假设真实网络请求出现超时了,方便演示出现ANR问题
            psersonBean = PsersonBean("测试用户02")
            LogUtil.i("刷新 耗时操作结束")
        }

        LogUtil.i("刷新 耗时操作 事件执行完毕")
        showData()

    }

    /**
     * runBlocking里即使使用 async 也会出现ANR问题
     */
    fun refreshByrunBlocking2() {

        runBlocking(context = Dispatchers.IO) {
            LogUtil.i("开始执行 刷新 耗时操作了")
            val psersonBean = async {
                delay(30000)//假设30秒,可以假设真实网络请求出现超时了,方便演示出现ANR问题
                PsersonBean("测试用户02")
            }
            LogUtil.i("刷新 耗时操作结束")
        }
        LogUtil.i("刷新 耗时操作 事件执行完毕")
        showData()
    }

    /**
     * 使用GlobalScope.launch ,没有出现ANR问题
     */
    fun refreshByGlobalScopeLaunch() {
        GlobalScope.launch(context = Dispatchers.IO) {
            LogUtil.i("开始执行 刷新 耗时操作了")
            delay(30000)//假设30秒,可以假设真实网络请求出现超时了,方便演示出现ANR问题
            psersonBean = PsersonBean("测试用户02")
            LogUtil.i("刷新 耗时操作结束")
            withContext(Dispatchers.Main){//切换到主线程更新UI
                showData()
            }
        }
        LogUtil.i("调用了 refreshByGlobalScopeLaunch 方法,没有阻塞当前线程")
    }

    fun detail() {
        LogUtil.i("执行了查看详情事件")
        psersonBean.moblie = "12345678901"
        showData()
    }
}

使用GlobalScope.launch解决ANR问题

点击页面上的 刷新按钮 调用 refreshByGlobalScopeLaunch方法,此方法里模拟了等待30秒耗时操作,当点击 刷新按钮 等待3秒左右,点击 详情按钮 ,页面数据正常显示如下图所示

刷新耗时操作结束,主线程更新UI

总结

  1. runBlocking主线程会一直 阻塞 直到 runBlocking 内部的协程执行完毕,执行长时间耗时操作会导致App应用出现ANR问题。
  2. runBlocking里即使使用 async 也会导致App应用出现ANR问题。
  3. GlobalScope.launch可以解决 耗时操作App应用出现ANR问题,注意需要配合withContext(Dispatchers.Main)进行更新UI操作

推荐

Kotlin:协程基础

相关推荐
扎克begod5 分钟前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python
code bean13 分钟前
【C#基础】函数传参大总结
服务器·开发语言·c#
阳光阿盖尔22 分钟前
EasyExcel的基本使用——Java导入Excel数据
java·开发语言·excel
蔚一25 分钟前
Java设计模式—面向对象设计原则(三) -----> 依赖倒转原则DIP(完整详解,附有代码+案例)
java·开发语言·设计模式·intellij-idea·依赖倒置原则
liang899930 分钟前
SpringSecurity原理解析(七):权限校验流程
java·开发语言
LQS202030 分钟前
基于Python实现一个浪漫烟花秀
开发语言·python
QXH20000032 分钟前
数据结构—单链表
c语言·开发语言·数据结构
梅如你32 分钟前
python批量对遥感影像进行归一化与数据清洗
开发语言·python
imaima66633 分钟前
数据结构----栈和队列
开发语言·数据结构
sinat_2765225737 分钟前
C++中move的使用
开发语言·c++