- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 146字
- 2021-08-05 10:46:49
What happens when the UI is blocked
It's a good opportunity to take a practical approach in order to further understand why the UI thread of an Android application should not be blocked. Let's add this simple block of code to MainActivity, and run the application again:
override fun onResume() {
super.onResume()
Thread.sleep(5000)
}
This will block the UI thread for five seconds. If you run the application again, you will notice how the screen is completely white during those five seconds. This would be dreadful for the user of the application.
Not only should the UI thread never be blocked, it should also not perform tasks that are CPU-intensive because this would result in a similar experience for the user. In short, you should only use the UI thread to create and update views, anything in between should be done in a background thread.
推薦閱讀
- Mastering Entity Framework Core 2.0
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Python自然語言處理實戰:核心技術與算法
- x86匯編語言:從實模式到保護模式(第2版)
- Mastering OpenCV 4
- R的極客理想:工具篇
- Mastering Rust
- 組態軟件技術與應用
- Microsoft Azure Storage Essentials
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Hands-On GUI Programming with C++ and Qt5
- Spring Boot實戰
- Troubleshooting Citrix XenApp?
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- 零基礎C#學習筆記