In depth analysis of the difference between service and thread in Android

Service is neither a process nor a thread. The relationship between them is as follows:

Some friends may ask, since it is a long and time-consuming operation, thread can also be completed. Yes, we can also complete a lot of time-consuming work in the program through thread, so what else do we need to do. Next, we will explain the following differences between service and thread.

First of all, the process is the smallest resource allocation unit of the system, while the thread is the smallest execution unit. The resources required by the thread are obtained through its process.

Difference between service and thread:

Thread: thread is the smallest unit of program execution. You can use thread to perform some asynchronous operations.

Service: service is a mechanism of Android. When it is running, if it is a local service, the corresponding service is running on the main thread of the main process. If it is a remote service, the corresponding service runs on the main thread of an independent process.

The operation of the thread is independent, that is, after an activity is finished, if the thread is not actively stopped or the run method in the thread is not executed, the thread will be executed all the time. Therefore, there will be a problem: after the activity is finished, it no longer holds the reference of the thread, that is, it can no longer control the thread. On the other hand, there is no way to control the same thread in different activities. For example, if a thread needs to connect to the server every other period of time to verify data, the thread needs to run in the background all the time. At this time, if the activity that created the thread ends and the thread does not stop, there will be no way to control the thread unless the process of the program is killed. At this time, if you create and start a service and create, run and control the thread in the service, you can solve this problem (because any activity can control the same service, and the system will only create an instance of the corresponding service). Therefore, you can think of service as a message service, and you can call context anywhere there is a context startService、Context. stopService、Context. bindService、Context. Unbindservice to control it, or you can register a broadcastreceiver in the service and send a broadcast to achieve the purpose of control, which thread can't do.

Many Android beginners may have such doubts. What is the relationship between service and thread? When should I use service and when should I use thread? The answer may surprise you a little, because there is no relationship between service and thread!

The reason why many people connect them is mainly because of the background concept of service. As we all know, thread is used to start a sub thread, where some time-consuming operations will not block the operation of the main thread. When we first understand service, we always think it is used to handle some background tasks, and some time-consuming operations can also be run here, which will cause confusion. However, if I told you that the service actually runs in the main thread, would you still think it has anything to do with the thread? Let's take a look at this cruel fact.

Add a line to the oncreate() method of mainactivity to print the current thread ID:

Then add a line to the oncreate() method of myservice to print the current thread ID:

Now run the program again and click the start service button. You will see the following print log:

You can see that their thread IDs are exactly the same, which proves that the service is indeed running in the main thread. That is to say, if you write very time-consuming code in the service, the program will have ANR.

You may exclaim, isn't this a pit father!? What's the use of my service? In fact, we should not associate the background with sub threads. These are two completely different concepts. The background of Android means that its operation is completely independent of the UI. Even if the activity is destroyed or the program is closed, the service can continue to run as long as the process is still running. For example, some applications that always need to maintain a heartbeat connection with the server can be implemented using service. You may ask, didn't you just verify that the service runs in the main thread? The heartbeat connection is always executed here. Won't it block the operation of the main thread? Of course, but we can create another sub thread in the service and deal with the time-consuming logic here.

Well, since a child thread should also be created in the service, why not create it directly in the activity? This is because it is difficult for the activity to control the thread. After the activity is destroyed, there is no other way to retrieve the instance of the child thread created before. Moreover, a child thread created in one activity cannot be operated by another activity. However, the service is different. All activities can be associated with the service, and then the methods can be easily operated. Even if the activity is destroyed, as long as the association is re established with the service, the binder instance in the original service can be obtained. Therefore, by using service to process background tasks, the activity can safely finish without worrying about the inability to control background tasks.

The above is all about the in-depth analysis of the differences between service and thread in Android brought by Xiaobian. I hope it will help you and support a lot of programming tips~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>