Code-Apprentice
Code-Apprentice

Reputation: 83587

Services, threads, and process

In a recent answer, I read

 A service does not run on its own thread, it runs on the UI thread

I've read similar statements in many other service-related questions.

If I call startService() from an Activity, will the Service run on the Activity's UI Thread or does it spawn a new process with it's own UI Thread?

If it runs on the Activity's UI Thread, doesn't that risk making the Activity unresponsive?

Upvotes: 2

Views: 316

Answers (5)

David Wasser
David Wasser

Reputation: 95646

In a recent answer, I read

 A service does not run on its own thread, it runs on the UI thread

This is just plain wrong. A Service is an object (an instance of a class). It doesn't run on any thread, it doesn't run at all. It just "is". The methods of the Service may run on any thread, depending...

Specifically, the lifecycle methods of a Service (onCreate(), onStartCommand(), etc.) that are called directly from the Android framework run on the main (UI) thread, which may or may not be the same thread that called startService(). However, a Service can (and usually does) start other background threads, as many as it needs to, to perform the necessary work.

If I call startService() from an Activity, will the Service run on the Activity's UI Thread or does it spawn a new process with it's own UI Thread?

See above. If you want your Service to run in a separate process, you can do this by specifying android:process=":service" in the <service> declaration in the manifest. By default, services run in the same process as the other components (Activity, BroadcastReceiver, etc.) of your application.

If it runs on the Activity's UI Thread, doesn't that risk making the Activity unresponsive?

That depends on what your Service is doing! Obviously if your Service is doing a lot of compute-intensive processing on the UI thread it will make your app unresponsive. If your Service is doing I/O on the UI thread, Android will usually throw exceptions (in more recent versions of Android). This is the reason that services usually start background threads to do work. However, if your Service is lightweight, then there is no reason why methods of your Service cannot run on the UI thread. As I said, it depends...

Upvotes: 1

earthw0rmjim
earthw0rmjim

Reputation: 19427

If I call startService() from an Activity, will the Service run on the Activity's UI Thread or does it spawn a new process with it's own UI Thread?

If it's a Service (not an IntentService), it will run on the main Thread of your app's process, as long as you do not define it as a separate process explicitly (by adding the android:process tag in the Manifest).

If it runs on the Activity's UI Thread, doesn't that risk making the Activity unresponsive?

Yes it does, if you perform CPU intensive/blocking operations.

To prevent that, you can either start new Thread(s) from your Service, or use an IntentService, which will automatically spawn a new worker Thread for its work.

Upvotes: 2

ashishb
ashishb

Reputation: 565

IntentService runs on the background thread, so, if you want to do one-off tasks then use that.

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37414

Yes services does run on main thread , About ANR(application not responding ) concern , there will higher chances of ANRs if your service is doing a long processing work using CPU so the official docs also recommends to use Worker Threads for efficiency. docs

You must read the caution paragraph in the docs to confirm

If your task is one time shot then Use IntentService which implicitly use threading mechanism for processing but IntentService will destroy itself automatically when the task is done so you will have to start it again for the next time and so.

android:process : Using this to start a service in an another process will cause usage of more memory. This will cause creating two processes for your app in separate heap memory space which also require other maintenance as well.So this is rarely used and i would not recommend to use this for a regular service task.

Upvotes: 3

Nikhil
Nikhil

Reputation: 3711

From the Services Docs it clearly explains your answer.

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

Upvotes: 1

Related Questions