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

2024-03-15 16:20

本文主要是介绍Kotlin:runBlocking导致App应用出现ANR问题实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

runBlocking简介

runBlocking 是常规函数;
runBlocking 方法会阻塞当前线程来等待;
runBlocking 的主线程会一直 阻塞 直到 runBlocking 内部的协程执行完毕。

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

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

页面布局activity_test_anr_by_runblocking.xml代码

<?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"><Buttonandroid: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" /><Buttonandroid: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" /><TextViewandroid: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代码

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

TestANRByRunBlockingActivity.kt代码

package example.demo.kotlin.activityimport 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.runBlockingclass TestANRByRunBlockingActivity : Activity() {private lateinit var tv_detail: TextViewprivate var psersonBean: PsersonBeaninit {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 延时初始化,这里安全起见,判断是否 isInitializedif (::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){//切换到主线程更新UIshowData()}}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:协程基础

这篇关于Kotlin:runBlocking导致App应用出现ANR问题实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/812544

相关文章

JAVA Calendar设置上个月时,日期不存在或错误提示问题及解决

《JAVACalendar设置上个月时,日期不存在或错误提示问题及解决》在使用Java的Calendar类设置上个月的日期时,如果遇到不存在的日期(如4月31日),默认会自动调整到下个月的相应日期(... 目录Java Calendar设置上个月时,日期不存在或错误提示java进行日期计算时如果出现不存在的

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

线程池ThreadPoolExecutor应用过程

《线程池ThreadPoolExecutor应用过程》:本文主要介绍如何使用ThreadPoolExecutor创建线程池,包括其构造方法、常用方法、参数校验以及如何选择合适的拒绝策略,文章还讨论... 目录ThreadPoolExecutor构造说明及常用方法为什么强制要求使用ThreadPoolExec

Nginx错误拦截转发 error_page的问题解决

《Nginx错误拦截转发error_page的问题解决》Nginx通过配置错误页面和请求处理机制,可以在请求失败时展示自定义错误页面,提升用户体验,下面就来介绍一下Nginx错误拦截转发error_... 目录1. 准备自定义错误页面2. 配置 Nginx 错误页面基础配置示例:3. 关键配置说明4. 生效

mysql_mcp_server部署及应用实践案例

《mysql_mcp_server部署及应用实践案例》文章介绍了在CentOS7.5环境下部署MySQL_mcp_server的步骤,包括服务安装、配置和启动,还提供了一个基于Dify工作流的应用案例... 目录mysql_mcp_server部署及应用案例1. 服务安装1.1. 下载源码1.2. 创建独立

Nginx服务器部署详细代码实例

《Nginx服务器部署详细代码实例》Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务,:本文主要介绍Nginx服务器部署的相关资料,文中通过代码... 目录Nginx 服务器SSL/TLS 配置动态脚本反向代理总结Nginx 服务器Nginx是一个‌高性

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

maven异常Invalid bound statement(not found)的问题解决

《maven异常Invalidboundstatement(notfound)的问题解决》本文详细介绍了Maven项目中常见的Invalidboundstatement异常及其解决方案,文中通过... 目录Maven异常:Invalid bound statement (not found) 详解问题描述可

idea粘贴空格时显示NBSP的问题及解决方案

《idea粘贴空格时显示NBSP的问题及解决方案》在IDEA中粘贴代码时出现大量空格占位符NBSP,可以通过取消勾选AdvancedSettings中的相应选项来解决... 目录1、背景介绍2、解决办法3、处理完成总结1、背景介绍python在idehttp://www.chinasem.cna粘贴代码,出