Android Call to ‘Thread.sleep()‘ in a loop, probably busy-waiting Inspection info:

Android studio 告警信息

Call to 'Thread.sleep()' in a loop, probably busy-waiting 
 Inspection info: Reports calls to java.lang.Thread.sleep() that occur inside loops. Such calls are indicative of "busy-waiting". Busy-waiting is often inefficient, and may result in unexpected deadlocks as busy-waiting threads do not release locked resources.

解决办法:替换为Timer及ScheduledExecutorService方案实现线程时长频率

问题分析

在这里插入图片描述

从代码中可以看到,告警位置特诊为循环中使用Thread.sleep(),且传入参数较短,
我们尝试去掉循环效果如下

在这里插入图片描述
可以看到黄色代码报警标志已经消失。


版权声明:本文为gao511147456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。