Question

Why android UI runs on main thread and not on other threads? Why is it only dedicated to UI ?

(Some will say its runs services as well, but i dont buy that. If that were to happen services will end when application exits, since main thread gets killed with process. According to me it runs in OS space( like windows services, i am guessing))

Why cant i change UI from other threads as well? (NOT posting work on UI thread) Is it the hardware constraint or what ? Why is it designed this way ?

Was it helpful?

Solution

Why cant i change UI from other threads as well?

Android was designed around the mobile CPUs of 2005-2007, which were about 2% of the power of today's CPUs. The synchronization overhead of trying to allow multiple threads to manipulate the UI was deemed to be too high. They used a common approach in those cases: designating a special thread to be the one for UI updates, and failing updates that are attempted on other threads.

Some will say its runs services as well

In general, objects do not run on threads -- methods run on threads.

Lifecycle methods on all Android components are called on the main application thread. So, for a Service, onCreate(), onStartCommand(), onBind(), and onDestroy() are called on the main application thread.

If that were to happen services will end when application exits

Applications in Android do not "exit".

since main thread gets killed with process

When a process terminates, all components in it also "get killed", and so a service that was in that process will go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top