Android process and thread

Source: Internet
Author: User

When a component runs for the first time, Android starts a process. By default, all components and programs run in this process and thread.

You can also schedule components to run in other processes or threads.

Process

The processes run by the component are controlled by the manifest file.The <activity>, <service>, <worker ER>, and <provider>-of a component contain a process attribute.This attribute allows you to set the processes for running the component: You can configure the component to run in an independent process, or multiple components to run in the same process. You can even run multiple programs in one process -- if these programs share a user ID and give the same Permissions. <Application> the Node also contains the process attribute.To set the default processes of all components in the program.

All components are instantiated in the main thread of this process.The system calls these components from the main thread. Not every object is separated from the main thread. In general, response methods such as view. onkeydown () user operations and notification methods are also run in the main thread. This indicates,When a component is called by the system, it should not run for a long time or be blocked (such as network operations or computing a large amount of data), because it will block other components in the process.. This type of operations can be separated from the main thread.

When more commonly used processes cannot obtain enough memory, Android may shut down infrequently used processes. The process will be restarted the next time you start the program.

When determining which process needs to be disabled, Android considers which process is more useful to users. For example, Android tends to close a process that is not displayed on the interface for a long time to support a process that is often displayed on the interface. Whether to close a process depends on the status of the component in the process. For more information, see component lifecycles.

Thread

Even if different processes are assigned to the component, the thread needs to be reallocated.For example, the user interface needs to respond quickly to the user, so some time-consuming operations, such as network connection, downloading, or operations that take up a lot of server time, should be placed in other threads.

The thread is created through the standard Java object thread. android provides a lot of convenient ways to manage threads:-A message loop is run in logoff thread; handler transmits a message; handlerthread creates a thread with a message loop.

Remote Procedure CILS

Android has a lightweight Remote Call (rpcs) mechanism. Through this mechanism, a method can be called locally and a value can be returned during Remote Execution (executed in other processes. To achieve this, you must break down the method call, and all the data to be passed must be accessible to the operating system. The local process and memory address are transferred to the remote process and memory address and processed and returned remotely. The return value must be passed in the opposite direction. Android provides the code for the above operations, so developers can focus on implementing RPC interfaces.

An RPC interface can only contain methods. All methods are executed synchronously.(Blocking is not completed until the remote method returns). This is also true if no return value is returned.

In short, this mechanism is as follows:Use IDL (Interface Definition Language) to define the interface you want to implement. The aidl tool can generate an interface definition for Java. This definition must be used both locally and remotely.. It contains two classes, see:

The inner class contains all the code required to manage remote programs (interfaces that comply with the IDL description. All inner classes implement the ibinder interface. One of them is used locally, regardless of its code. The other is called stub, which inherits the binder class. To implement remote calls, This class includes the RPC interface. Developers canInherit stub class to implement required methods.

Generally, a remote process is managed by a service (because the service can notify the operating system of the process and communicate with other processes). It also contains the interface file generated by the aidl tool, stub class implements the method in the distance. The service client only needs the interface file generated by the aidl tool.

The following describes how to connect to the service and call the client:

  • · The service client (local) implements the onserviceconnected () and onservicedisconnected () methods. In this way, you can get notifications when the client is connected or disconnected. Obtain the connection to the service through bindservice.
  • · The onbind () method of the service can receive or reject connections, depending on the intent it receives (the intent connects to the service through the bindservice () method ). if the service receives a connection, a stub instance is returned.
  • · If the service accepts the connection, Android will call the onserviceconnected () method of the client and pass an ibinder object (system-managed stub-class proxy). Through this proxy, the client can connect to a remote service.

The above descriptions omit many RPC mechanisms. See the designing a remote interface using aidl and ibinder classes.

Thread Security Method

In some cases, a method may call more than one thread, so pay attention to the thread security of the method.

Pay attention to this for methods that can be called remotely. When a program that calls methods in the ibinder object starts the same process as the ibinder object, the method is executed in the ibinder process. However, if the caller initiates another process and the method runs in another thread, this thread is in the same thread pool with the ibinder object; it will not run in the main thread of the process. For example, if a service is called the onbind () method from the main thread, the method in the object returned by onbind () (for example, the stub subclass implementing RPC) will be called from the thread pool. Because a service may have multiple client requests, more than one thread pool will call the ibinder method at the same time. Therefore, ibinder must be thread-safe.

Simply put, a content provider can receive data requests from other processes. Even if the contentresolver and contentprovider classes do not hide the details of managing interactions, the methods (query (), insert (), delete (), update (), and GetType ()-it is called in the content provider thread pool, rather than the contentprovider process. These methods may run from a very multi-threaded pool at the same time, so these methods must be thread-safe.

Related Article

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.