lovespring
lovespring

Reputation: 19569

android component and thread, which thread to run?

android don't have a "main" method, so all component of android app, such as activity, content provider can be run by the android system in response to other app's request.

So, suppose at the nearly sometime, two app request two component of ONE app.

my question is will those component running in one thread or different thread?

Upvotes: 1

Views: 110

Answers (2)

pomber
pomber

Reputation: 23980

Most interactions between the OS and the application run on the main thread (AKA UI Thread). It doesnt matter how many "requests" the app receive at the same time, all the "requests" are processed by a looper in the main thread.

You could add tasks to this main thread looper from any thread in your app:

Runnable task = buildYourTask();
new Handler(Looper.getMainLooper()).post(task);

[Update]
This is from android developer documentation:

The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006779

Components do not run on threads. Objects do not run on threads. Methods run on threads.

The lifecycle methods of a component, such as onCreate() of an activity, run on the main application thread.

Upvotes: 0

Related Questions