Service is one of the four components of the Android. It is used to perform background tasks for a long time. Similarly, threads can also perform tasks in the background, what are the differences between them? When to use Service when to use Thread ? Today I will also talk about my understanding and summary.
First, we need to understand several characteristics of Service.
(1) by default, Service is actually running in the main Thread. To perform complex and time-consuming operations, you must create another Thread in the Service to execute the task.
(2) the priority of the Service is higher than the Activity suspended in the background, and of course, it is also higher than the Thread created by the Activity. Therefore, the system may kill the Activity or Thread in the background first when the memory is insufficient, service component is not easily killed. Even if the Service is forced to be killed, the killed Service will be restarted when resources are available.
In fact, Service and Thread are not at the same level at all. Service is one of the four major components of the system. Thread is just a tool class used to execute background tasks. It can be created in Activity, you can also create a Service. Therefore, we should not discuss whether to use Service or Thread, but where to create a Thread.
In a typical application, it can be created in the following three locations, different position , its life cycle is different, so, we should decide whether to create a Thread in the Service or in the Activity according to the target lifecycle of the Thread.
(1) created in Activity
in this case, the Thread is generally created during onCreate and destroyed in onDestroy(). Otherwise, after the Activity is destroyed, the Thread still runs in the background.
In this case, the lifecycle of the Thread is the lifecycle of the entire Activity. Therefore, the Thread created in the Activity is only suitable for tasks related to the dependent Activity, such as regularly updating the control status of the Activity.
Core features: the Thread serves the Activity, completes the tasks assigned by the specific Activity, and actively notifies the Activity of some messages and events. After the Activity is destroyed, the Thread is also meaningless to survive.
(2) created in Application
in this case, the custom Application class, overloads the onCreate method, and creates a Thread in it. Of course, the Thread is also destroyed in the onTerminate() method. Otherwise, if the Thread does not exit, even if the entire Application exits, the Thread still runs in the background.
In this case, the lifecycle of the Thread is the lifecycle of the entire Application. Therefore, the Thread created in the Application can perform some Application-level tasks, such as checking the network connection status regularly.
Core features: the ultimate goal of the Thread is to serve various Activity of the APP, including completing tasks assigned by a Activity and actively notifying a Activity of some messages and events, after the APP exits, the Thread does not exist.
In both cases, the lifecycle of the Thread should not exceed the lifecycle of the entire application. That is, after the entire application exits, the Thread should completely exit, this prevents memory leaks or zombie threads. If you want the Thread to run after the entire APP exits, you should put the Thread into the Service to create and start it.
(3) created in Service
this is the only way to ensure the Thread with the longest lifecycle. As long as the entire Service does not exit, the Thread can always run in the background. Generally, it is created in the Service () of the onCreate, and in the onDestroy() destroy.
Therefore, the Thread created in the Service is suitable for running background tasks independent of the APP for a long time. It is common to maintain a long connection with the server in the Service.
Core features: the Thread can provide some "services" or "status queries" for the APP, but the Thread does not need to actively notify the APP of any events or even know who the APP is.
In a word, we should not consider using Thread or Service, but choose an appropriate lifecycle for Thread. This is what I think and understand about Service and Thread.
This article is forwarded from Jhuster 51CTO blog, original link: http://blog.51cto.com/ticktick/1547032,如需转载请自行联系原作者
Start Building Today with a Free Trial to 50+ Products
Learn and experience the power of Alibaba Cloud.
Sign Up Now