Suresh
Suresh

Reputation: 9605

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

Curious to know the reason behind not allowing updating 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?

Upvotes: 1

Views: 283

Answers (1)

Pontus Gagge
Pontus Gagge

Reputation: 17278

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.

Upvotes: 3

Related Questions