Android Development thread and process explanation

Source: Internet
Author: User
Tags stub

  process: The process that the component is running is controlled by manifest file. Component Nodes <activity>, <service>, <receiver>, and <provider> all contain a process attribute. This property can set the process that the component is running: You can configure the component to run in a stand-alone process, or multiple components run in the same process. You can even run multiple programs in one process-if the programs share a user ID and give the same permissions. The node also contains process attributes that are used to set the default processes for all components in the program.

All components are instantiated in the main thread of the process, and the system calls the components apart from the main thread. Not every object will be detached from the main thread. In general, methods and notifications for responses such as View.onkeydown () user actions are also run in the main thread. This means that when a component is called by the system, it should not run for long periods or block operations (such as network operations or compute large amounts of data), because it can block other components in the process. This type of operation can be separated from the main thread.

When more commonly used processes do not get enough memory, Android may shut down infrequently used processes. The process restarts the next time the program is started.

When deciding which process needs to be shut down, Android will consider which is more useful to the user. Android, for example, tends to shut down a process that does not appear in the interface for a long time to support a process that is often displayed in the interface.

  Threads: Even if a different process is assigned to a component, it is sometimes necessary to redistribute the thread. For example, the user interface needs to respond quickly to the user, so some time-consuming operations, such as network connections, downloads, or very intensive server-time operations, should be placed on other threads.

Threads are created through Java's standard object thread. Android offers a number of handy ways to manage threads:-looper A message loop in a thread; Handler pass a message; Handlerthread creates a thread with a message loop.

Remotely invoke remote procedure calls

Android has a lightweight mechanism for remote calls (RPCs)-through this mechanism, methods can be invoked locally, executing remotely (in other processes), and returning a value. To implement this requirement, the method call must be decomposed, and all data to be passed must be a level that the operating system can access. Transfers from local processes and memory addresses to remote processes and memory addresses and is processed and returned remotely. The return value must be passed in the opposite direction. Android provides the code to do the above, so developers can focus on implementing RPC interfaces.

An RPC interface can only contain methods. All methods are synchronized (until the remote method returns and the local method ends blocking), as well as when there is no return value.

In simple terms, the mechanism is this: using IDL (Interface Definition Language) to define the interface you want to implement, the Aidl tool can generate interface definitions for Java, both locally and remotely. It contains 2 classes, as shown in the following figure:

The inner class contains all the code needed to manage the remote program (which conforms to the IDL-described interface). All inner classes implement the IBinder interface. One is used locally, regardless of its code, and another called stub inherits the Binder class. In order to implement a remote invocation, this class contains the RPC interface. Developers can inherit stub classes to implement the desired method.

In general, a remote process is managed by a service (because the service notifies the operating system of the process and communicates with other processes), it also contains the interface file generated by the Aidl tool, and the stub class implements the far way. The client of the service needs only the interface files produced by the Aidl tool.

The following is how to connect services and client calls:

• The client (local) of the service implements the Onserviceconnected () and onservicedisconnected () methods so that notifications can be obtained when the client connects or disconnects. Gets the connection to the service through Bindservice ().

· The Onbind () method of a service can receive or reject a connection, depending on the intent it receives (intent connect to the service through the Bindservice () method). If the service receives a connection, an instance of the stub class is returned.

· If the service accepts a connection, Android invokes the client's onserviceconnected () method and passes a IBinder object (a proxy for the system-managed stub class) through which the client can connect to a remote service.

The above description omits many RPC mechanisms. See Designing a Remote Interface Using aidl and IBinder classes.

Thread-Safe methods

In some cases, a method might call more than one thread, so you need to be aware of the thread safety of the method.

Note this also for methods that can be invoked remotely. When a program that invokes a method in a IBinder object starts the same process as the IBinder object, the method executes in the IBinder process. However, if the caller initiates another process, and the method runs in another thread, the line Cheng the IBinder object in a pool of threads; it does not run in the main thread of the process. For example, a service is called from the main thread to the Onbind () method, and the Onbind () returned objects (such as the stub subclass that implements RPC) are invoked from the thread pool. Because a service may have multiple client requests, more than one thread pool invokes the IBinder method at the same time. Therefore, IBinder must be thread safe.

In simple terms, the mechanism is this: using IDL (Interface Definition Language) to define the interface you want to implement, the Aidl tool can generate interface definitions for Java, both locally and remotely. It contains 2 classes, as shown in the following figure:

The inner class contains all the code needed to manage the remote program (which conforms to the IDL-described interface). All inner classes implement the IBinder interface. One is used locally, regardless of its code, and another called stub inherits the Binder class. In order to implement a remote invocation, this class contains the RPC interface. Developers can inherit stub classes to implement the desired method.

In general, a remote process is managed by a service (because the service notifies the operating system of the process and communicates with other processes), it also contains the interface file generated by the Aidl tool, and the stub class implements the far way. The client of the service needs only the interface files produced by the Aidl tool.

The following is how to connect services and client calls:

• The client (local) of the service implements the Onserviceconnected () and onservicedisconnected () methods so that notifications can be obtained when the client connects or disconnects. Gets the connection to the service through Bindservice ().

· The Onbind () method of a service can receive or reject a connection, depending on the intent it receives (intent connect to the service through the Bindservice () method). If the service receives a connection, an instance of the stub class is returned.

· If the service accepts a connection, Android invokes the client's onserviceconnected () method and passes a IBinder object (a proxy for the system-managed stub class) through which the client can connect to a remote service.

Thread-Safe methods

In some cases, a method might call more than one thread, so you need to be aware of the thread safety of the method.

Note this also for methods that can be invoked remotely. When a program that invokes a method in a IBinder object starts the same process as the IBinder object, the method executes in the IBinder process. However, if the caller initiates another process, and the method runs in another thread, the line Cheng the IBinder object in a pool of threads; it does not run in the main thread of the process. For example, a service is called from the main thread to the Onbind () method, and the Onbind () returned objects (such as the stub subclass that implements RPC) are invoked from the thread pool. Because a service may have multiple client requests, more than one thread pool invokes the IBinder method at the same time. Therefore, IBinder must be thread safe.

In simple terms, a content provider can receive data requests from other processes. Even though the Contentresolver and ContentProvider classes do not hide the details of administrative interactions, the methods in ContentProvider that respond to these requests (query (), insert (), delete (), update (), and GetType ())-is invoked in the thread pool of the content provider, rather than the contentprovider itself process. Because these methods may be run from many thread pools at the same time, 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.