- 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.
推薦閱讀
- Learning Spring 5.0
- JIRA 7 Administration Cookbook(Second Edition)
- Ext JS 4 Web Application Development Cookbook
- Kotlin從基礎(chǔ)到實(shí)戰(zhàn)
- 用戶(hù)體驗(yàn)可視化指南
- Node Cookbook(Second Edition)
- 基于SpringBoot實(shí)現(xiàn):Java分布式中間件開(kāi)發(fā)入門(mén)與實(shí)戰(zhàn)
- Arduino可穿戴設(shè)備開(kāi)發(fā)
- Web App Testing Using Knockout.JS
- 從Power BI到Analysis Services:企業(yè)級(jí)數(shù)據(jù)分析實(shí)戰(zhàn)
- 算法圖解
- Visual C++從入門(mén)到精通(第2版)
- 從零開(kāi)始學(xué)Python大數(shù)據(jù)與量化交易
- AngularJS UI Development
- Mastering Responsive Web Design