Question Detail

Difference between thread and service

6 years ago Views 9753 Visit Post Reply

What is the difference between a Thread and services? As both doing the same thing in different way. difference between Threads and Services when both are employed to perform a similar task


Thread Reply

Anonymous

- 6 years ago

SERVICE: Android beginners often assume that services run in a separate thread because they are designed to run background tasks. This is not the case! By default, a service will be called on the main thread. This confusion often arises because the main thread is referred to as UI thread and services are supposed to run in the background.

THREAD: Creating threads in Android is essentially the same as in any other Java application. You can implement the Runnable interface and you can pass it to a Thread to execute. The question that arises is where the thread should be managed.

 

Difference Between Thread and Service in Android

Thread Service

O.S level feature that allows you to do background process. 

long-running operation in the background, mostly without having UI

Thread Run on its own Thread

Service Run on Main Thread

A thread is a lightweight process.It is not an android component.We cannot update a UI thread, we need a handler for this.

Service is a component of Android.It is also an Activity but has no interface.

Threads run in the lifecycle of Activity, and are killed/stopped when an Activity is destroyed.

Services run in the background, have their own life cycle that is independent of the life cycle of the Activity. So when an Activity is destroyed, services still run, unless they are stopped explicitly.

 

 

 

 

 

 

 

 

 

 

 

 

Some More Difference between SERVICE , THREAD  , INTENTSERVICES and ASYNCTASKCapture

Nick Johnson

- 6 years ago

Thread
A thread should be used in a long-running process that would block the UI from updating.

You might want to put it into a background thread and notify the user with a dialog or spinner or something.

 

Service

Services will block the OS's UI thread

you can spawn a new thread within a service. A service is used more for something that should happen on an interval or keep running/checking for something when there is no UI shown.

The Service runs on the main thread, and therefore you should offload the work to an AsyncThread or something like that inside the Service. From not having a UI does not follow that it is not or may not or won't be run in the main thread. You can generally assume that, but not always.