开发者

Why is it required to update the view only from application main thread?

开发者 https://www.devze.com 2023-01-20 15:13 出处:网络
Curious to know the reason behind not allowing updating开发者_Python百科 UI elements from background thread in Android.

Curious to know the reason behind not allowing updating开发者_Python百科 UI elements from background thread in Android.

Will main thread does something more (probably interacting with framework) after updating the UI elements so that changes can be seen on the screen.?

Is it the same case with other GUI tool kits?


If all threads were permitted to update the GUI objects, they'd have to be designed for thread safety (since the GUI must track its underlying state), introducing locks or critical sections around member variables and other shared resources. This would

  • slow the GUI down
  • complicate the code,
  • not be 100% safe anyway.

Concurrency is hard, and any framework designer has to compromise. Now the burden is on you to ensure things happen in the right thread. You should isolate worker and communications tasks from the UI anyway, so it doesn't really add all that much of an onus.

0

精彩评论

暂无评论...
验证码 换一张
取 消