Take the 2-minute tour ×

I am confused with android services and java thread.

Please help me to understand in which scenario i should use them.

As per my understanding

Service run in background ,so do thread.

Service is to be used for activities like network operation or playing mp3 in background,so do threads.

So whats actual difference between them and when to use each of them.

share|improve this question
I am not sure if your question is restricted to Android, it is a matter of difference between threads and processes generally (I imagine android services are separate processes that are maintained by the OS when you own process is inactive/killed/stopped...) –  Ali Mar 15 '12 at 21:01
It is specific to Android. Service is a component of application. It is the term of Android. Android's components of application are very specific processes. Services are more specific. Threads in Android are specific, too, you can't use them in some cases(GUI). –  Gangnus Mar 15 '12 at 21:04
add comment

6 Answers

Let me give an analogy.

Activities and Service are like projects.

Activities are like external projects. This is what the clients(users) see.

Services are like internal projects. There might be several internal projects for 1 external project or none at all.

You can "pause" external project but the internal project that supports it can still continue.

Main Thread is like the boss in a company

The boss should never be held up by too much work since he shouldn't be late to meetings (UI freezing) or the client(user) will be unhappy.

Threads are like employees in a company.

The more you have, the more things you can do at the same time provided you have enough equipment(CPU speed) for all of them.

Multiple employees can work on the same project at the same time but the boss should really work only on the Activities.

share|improve this answer
add comment

Services are more analogous to a headless Activity.

The the important piece to understand is that a Service is about managing application lifetime and the ability to keep work running when your Application is not in the foreground (no UI visible). It is also about providing the ability to expose functionality to other apps.


Typically when starting a Service you will also start a worker Thread. There are settings in the manifest that can cause a Service to be started in a new Process but generally you do not need to do this, it makes communication with your service more difficult.

Use a just Thread in your Activity when you need to offload work from the UI thread while the application is in the foreground, but this work can stop when you are no longer in the foreground. (It is possible that your app will continue to run when not it foreground but there is no guarantee depending on a number of factors) Generally speaking Android is free to kill your Activity if it is not in the foreground, and if your App process has no Activities or Services it can be killed.

Use a Service with a Thread to do work that will take place while your app is in the background and you want better guarantee about the lifetime.

Use a Service to expose non-UI functionality to other applications.

share|improve this answer
add comment

I believe the main difference is about Android system attitude. Service is a part of android infrastructure, so android recognizes service as a working part of application and considers killing service as a last option. Moreover, you can tune up service priority in order to do it as important as foreground activity. As for threads, android does not recognize a thread as important part which must be kept. So usual threads has much more chances to be killed.

For instance If you have an activity which start a working thread and then go background, as android do not recognize thread as a working part, it may think that application do nothing, because no activity or service running and kill the whole app, including the working thread.

Thus when you start a Service, you are telling system something like: "Hi. I'm doing some business here, don't kill me until I finish, please." and Android pay attention to your request.

share|improve this answer
add comment

Always: A service of your application is usable not only by other components of your application, but by other applications, too.

Mostly: A service is something more independent, than a thread. Service is something more long-living than a thread. Service is something more complex than a thread.

What they have in common: they both are for use in not-GUI parts of program.

BTW, threads do not run in background only. What runs in foreground, is a thread, too, usually.

share|improve this answer
add comment


Use services to do things like pulling more than one network resource (downloading multiple images, synchronize with server) or listening to sensors, or in need of handling multiple intents from different contexts.....not as flexible as using Handler in my opinion, but you get to notify all the observers instead of just the caller.

Use threads to do things like processing one image, or downloading one image, or query the database, etc. That they are single focused on one task.


Services are longer lived and is independent of your current context. It is more flexible than a thread as it can spawn its own threads safely, etc. It can manage its own lifecycle and interact with other services, etc. You will need to use boardcasts to notify the caller.

Threads are used to do short-lived tasks and is controlled by the process that started it. You can use a Handler in this case to post to the caller for updates.

Behind the scenes:

Services are running on separate processes than the activity that wanted to start it, so it won't die if your main activity dies. (it will have a different pid). In fact, it is the operating system that started the service, not the activity context.

Threads are spawn within a certain process. (it will have the same pid as the process that spawns it). So the process can easily interrupt it and if the process dies, the thread dies as well.

share|improve this answer
add comment

Android Service don't run in a separate process (by default) and even don't run in a separate thread! It runs in the main thread(UI thread) of the application, therefore if you would like to do something time consuming task in the Service start a separate thread yourself, or use IntentService.

share|improve this answer
add comment

Your Answer


