What is the difference between service and thread? Advantages and disadvantage of Service and Thread.

What is the difference between service and thread

In android, the main two components are service and thread. Both two are used to enhance the application user experience. Also, these are used to control the long-running operation and background multitasking. But in android, many of the people have confused between service and thread because of the working procedure and uses. Here in this blog, we will discuss what is service and thread with their difference. In the end, you will clear idea about the uses of service and thread in android.

what is the thread?

Ans. In android, some times application has long-running operations and these operations are not executed in the main thread. So that Threading concept is introduced which handle the time consuming or long operation.

Types of thread

Thread have two type i,e, UI thread or main thread and the second one is the background thread

UI thread

The UI thread is operating regarding the UI(User Interface) like rendering the UI component, update the changes in UI, etc. which take a lot of processing.

Background Thread

The background thread is those thread which executes the task apart from the UI thread or Main thread that Thread is known as the background thread.

Why we need Thread?


Ans. In android, the processing of the task is by default execute in the main thread so that the main thread has two work one refreshes the application screen and second was the process the given task. That makes our application hang and sometimes the app will be crash.

So the andriod design introduces a concept called Threading. It gives the facility to execute the processing smoothly without block the UI. It just takes the task and creates a background thread and process the instruction so that UI thread refresh the screen and background the processing another task.

What is Service in android?

Ans. Service the component present in the android which is used to perform the long-running operation even the application is destroyed and user not interacting with the application.When the service is started even if the application is destroyed still the processing continues. It is used in the networking of application which plays in the background and information to the application. 

For example: In WhatsApp application when the internet is on then someone sends the message then the notification will show in notification panel although WhatsApp is close.

What is the need for service?

Ans. In the thread, we operate in the background but when the application is destroyed then Thread is destroyed after completing the operation. But we need a concept hereafter destroyed application in the background some operation is executed. So that service is introduced.

Types of service


Following are the type of service.

  1. Schedule service
  2. service
  3. intent service
  4. Bound service

1.Schedule service:-

Schedule service is the service active when a certain condition is fulfilled. 
for example:- When 10'o clock the alarm start. This type of scheduling the time to activate the service then we need the Schedule service.

2.Service:-

This the basic service which will give the functionality to run the task in the background. It just             takes the task and runs in the background until the user does not close the service.

3.Intent service:-

Intent service is the update version of the basic standard service. Here all type of operation like handling the task with close after the complete operation. Intent service has the feature to return the process data to the  UI.
for Example:-
This type of service is used for networking purpose like transmit request receive data etc. 

4. Bound Service:-

Bound service is used to continuously communicate with the service and the UI component. Here service is bind with the one or more UI component and communicates like client-server architecture. When all the bind UI component which is blind to particular service is destroyed then the service destroyed.

Difference between service and thread

 Service                                                                     Thread                                                                    
 It is has no UI.

 It has UI
 Use in long running operations

 use small operation
 work after application destroyed.

 When application destroyed is all thread cancel
 Three type of Service
  1. background
  2. foreground
  3. Bound    
 Only two type UI thread and Background thread
 service use with thread to give better smooth user experience

 On thread can use to execute the task
 Here use Intent service,Bound service,schedule service and service

 Here use Asynktask
 It may block in main thread(without using the Thread)

 Main thread not block because it use on separate thread called background thread
 To perform a smooth operation it needs Background thread.

 No need for any third party for better performance
 Service may be blocked by android because of unless taking the memory and space.

 The thread doesn't block android
 To active service, we need Broadcast Receiver like when Phone Boot service is run. To active thread with the UI component

Post a Comment

Previous Post Next Post

Recent Posts