What is the difference between service and thread?

Source: Internet
Author: User

A: servie is a system component hosted by system processes (servicemanager). The communication between them is similar to client and server, and is a lightweight IPC communication, the carrier of this communication is binder, which is an IPC for information exchange at the Linux layer. Thread is hosted by this application.

 

1). Thread: thread is the minimum unit for program execution. It is the basic unit for CPU allocation. You can use thread to perform some asynchronous operations.

2). Service: A mechanism of Android. If it is a local service during running, the corresponding service runs on the main thread of the main process. For example, oncreate and onstart functions are in the main process when called by the system.
Run on the main thread. For remote service, the corresponding service runs on the main thread of the independent process.

In this case, why should we use the service?
What about it? In fact, this is related to the Android system mechanism. Let's take the thread first.
. The thread runs independently of the activity. That is to say, after an activity is finished, if you do not stop the thread
Or if the run method in the thread is not completed, the thread will continue to execute. Therefore, a problem occurs: after the activity is finished, you no longer hold the thread
. On the other hand, you cannot control the same thread in different activities.

 

For example, if your thread needs to connect to the server for some synchronization after a period of time, the thread needs to run when the activity does not start. When you start
The activity cannot control the previously created thread in the activity. Therefore, you need to create and start a service, create, run, and control the thread in the service, so as to solve the 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 a service as a message service, and you can call context wherever context exists. startservice, context. stopservice, context. bindservice, context. unbindservice, to control it, you can also
Register broadcastreceiver in the service and control it by sending broadcast elsewhere. Of course, these are all threads that cannot be used.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.