Is an Android Service a process or thread? - android

Is an Android "Service" considered a process or a thread according to operating system?

It is neither, any more than an activity is "a process or a thread".
All components of an Android application run inside a process and by default utilize one main application thread. You can create your own threads as needed.

A Service can be implemented as a separate process depending on the requirement.
Here is a good explanation when to implement the service as a separate process or as a component inside an existing app (Service with Activities in same process).

Service is not a process nor a thread. Its a part of the process and a main thread. Like all other components - service runs in the main thread.. Lets say - your application has an Activity and a Service then your Application is a unix process which has a process id (pid) and runs in a instance of DVM... Process spawns a first thread which is main thread.. All your application components runs in main thread by default - you can call separate thread to do parallel jobs.

Well, processes host services in the Android OS, so it's not a process. Maybe have a look at the definition of a Service for more details...

Related

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?
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.
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.
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.
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...
IntentService runs on the background thread, so, if you want to do one-off tasks then use that.

Android Process and Resource managment

As Per My Knowledge, In Android when Application is start , Os assign new process to app and app will running in that UIThread (mainthead) of that process.
so operation which is define under Activity Class will run on UiThread ( if we will not create separate class for any operation). and if we perform long running process we will do that task under the Service class ( Service is running even App will kill ).. So Service is running on UiThread or separate Thead? if it runs under UiThread then Why it is not affect to Ui OR if it runs under separate Thread then why we are creating another separate thread in service for long running operations?
In Android when Application is start , Os assign new process to app
Correct.
app will running in that UIThread (mainthead) of that process
Incorrect. An "app" does not run on any thread. Methods of Java objects are called on threads.
so operation which is define under Activity Class will run on UiThread
Incorrect. A Java class does not run on any thread. Methods of Java objects are called on threads. Hence, methods of an activity will be called on threads. Some of those methods will be called on the main application thread (a.k.a., UI thread) of the Android app's process.
So Service is running on UiThread or separate Thead?
Neither, or both, depending upon how you want to look at it. Again, a Java class does not run on any thread. Methods of Java objects are called on threads. The lifecycle methods of a service (e.g., onCreate(), onStartCommand(), onDestroy()) are called on the main application thread. onHandleIntent() of an IntentService is called on a background thread.
if it runs under UiThread then Why it is not affect to Ui
Any time you spend in your code on the main application thread will freeze the UI. It does not matter whether the code is a method on an activity, a method on a service, or a method on something else.
if it runs under separate Thread then why we are creating another separate thread in service for long running operations?
To avoid tying up the main application thread and thereby freezing the UI.
Services runs on UI thread. Also in some way it can interact with UI.
Here is a good starting article.
http://developer.android.com/guide/components/services.html

Service + android:process "vs" IntentService

As we know difference between Service and IntentService is that IntentService implicitly spawn worker thread whereas Service run on process main (UI) thread.
That is apparently showing what the difference between them.
But my question is that is there any difference between
Service + android:process "vs" IntentService
I mean to say if we launch Service in a separate process than still any difference between them except Service uses Process whereas IntentService uses Thread.
You are completely misundertand the Concept of Process and Thread.
IntentService run in a diffrent Thread but run in main process.
For Every Application by default there is a only one process,in which all of your code will run including Services,Activitys And IntentService too.
When ever you start your Service in a new process then that Service has allocated with separate memory and heap area.It will not directly communicate with the main process.For communication purpose you need to use AIDL
For more info check this Process and Threads

Within an application, will the activity and service run in the same process?

Interview Question : With in an Application, will the Activity and Service run in the same process or different processes?
My Answer was ::: In the same process.
Next Question ::: If so, how is that the Activity and Service run at the same time?
My Answer was ::: Operating System will take care of the execution. (Frankly, I didn't knew the answer).
Can someone give an explanation of the above questions? If my answer was wrong, what is the correct answer?
If service and activity belongs to your app then:
Same process if not defined otherwise. You can create service that will run in separate process.
Service and Activity share same thread. So they can't run simultaneously. But you can create new threads to process commands in Service. Or use IntentService that processes all commands in own thread. Some Service methods always executed on UI thread (e.g. onCreate) Then activity and service can run in parallel (if you have 2 and more cores =).
they can both work in same or different processes depending on customization. and yes, OS is the responsible for execution of these two. check this for detailed explanation: http://developer.android.com/guide/topics/manifest/service-element.html#proc
There is no way to start an Activity and a Service that run in the same process (which is the default behaviour) at the same time.
The onCreate() method of the Service runs on the main (UI) thread.
The onCreate() method of the Activity also runs on the main (UI) thread.
The only way to have both start at more-or-less the same time is to have the Service run in a separate process.
You can try to launch few threads and use semaphore with barier flag.

Android Service and thread

Is a "Service" in android same as a separate thread ?
From Service javadoc:
Note that services, like other
application objects, run in the main
thread of their hosting process.
So the answer is No.
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

Categories

Resources