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.
精彩评论