The difference between Android-service and thread

Source: Internet
Author: User

1. Services are not a single process. The service does not own a process, the application can be different, and the service runs in the same process.

2. The service is not a thread. Can work in a thread.

I. In the application, use the service if it is running in the background for a long time and does not require interaction.

It is also run in the background, without the need for interaction, if you just complete a task, then you do not need to run it, and may be multiple tasks that require a long running time to use the thread.

Two. If the task consumes more CPU time and the resource is large, use the thread.

Servie is a component of the system, which is managed by the system process (ServiceManager); their communication is similar to client and server, is a lightweight IPC communication, the carrier of this communication is binder, it is an IPC for exchanging information at the Linux layer. Thread is hosted by this application.

1). Thread:thread is the smallest unit of program execution, and it is the basic unit for allocating CPUs. You can use the Thread to perform some asynchronous operations.

2). Service:service is a mechanism for Android, and when it runs, if it is a Local service, then the corresponding service is running on the main thread of the master process . such as: Oncreate,onstart These functions are run on the main thread of the master process when they are called by the system. In the case of Remote service, the corresponding service is running on the main thread of the standalone process .

In that case, why should we use Service? Actually this is related to the Android system mechanism, we first take the thread to say. The thread runs independently of the activity, meaning that if you do not actively stop the thread or the Run method in thread does not complete after an activity is finished, thread will always execute. so there's a problem here: When the Activity is finished, you no longer have a reference to that Thread. On the other hand, you have no way to control the same Thread in different Activity.

For example, if your thread needs to be connected to a server for some sort of synchronization over time, the thread needs to be running when the Activity does not start. This time when you start an activity there is no way to control the Thread that was created in the activity. So you need to create and start a service, create, run, and control the Thread within the service, which solves the problem (because any Activity can control the same service, and the system will only create an instance of the corresponding service).

So you can think of the service as a message, and you can call Context.startservice, Context.stopservice, Context.bindservice,context.unbindservice, to control it, you can also register Broadcastreceiver in the Service, in other places by sending broadcast to control it, of course, these are The Thread cannot do that.

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.