Android Multithreading - Preliminary Multithreading, Detailed Explanation of Process Threads

From this part, we start to learn Android's multi-threading. Before that, we need to figure out two concepts, process and thread.

When Android starts an application, if the application has no other components running, the system will start a new linux process for the application, and start a UI thread (main thread) by default in this process. By default, all components of the same application run in the main thread of the same process. If an application component starts, and a process for that application already exists (because other components in the application have already started), the component will start in that process, using the same main thread. However, you can set up different components of your application to run in separate processes, and you can create additional threads for any process. Next, we describe processes and threads in detail.

Process

Each App must create a process before it starts. The process is forked by Zygote. The process has an independent resource space and is used to carry various Activity/Service components running on the App. In most cases, an App runs in a process, unless the Android:process attribute is configured in AndroidManifest.xml, or the process is fork through native code.

thread

By default, each Android application has only one thread (UI thread, that is, the main thread). The main thread is responsible for the display, update, control interaction, etc. of the UI interface of the application. Therefore, some time-consuming operations (network access, download operations, Querying the database, etc.) cannot be placed in the main thread to avoid blocking the main thread.

ps: Android stipulates that the UI update operation can only be performed in the UI thread.

Since Android can only operate UI controls in the UI thread, and cannot perform time-consuming operations in the main thread, then the problem arises. If we need to display data on the UI after accessing the network, what should we do? Create a child thread to access the network. How to notify the main thread to update the UI when the data acquisition is completed, which involves the multi-threaded communication we are going to talk about.

multithreaded communication

To perform multi-threaded communication, there are several ways in Android:

  1. activity.runOnUiThread(Runnable runnable); The method in Activity is called in the child thread, and the UI update operation can be performed in the method.
  2. view.post(Runnable runnable); view.postDelayed(Runnable runnable, long delayMillis); The method in View to perform UI operations, or to perform UI operations after a delay
  3. BroadCast uses the Android broadcast mechanism in combination with BroadCastReceiver to send an update broadcast in the child thread, receive the broadcast in the main thread and perform corresponding operations. It is not used much, just understand it.
  4. AsyncTask uses AsyncTask to open asynchronous tasks to update UI in child threads **
  5. Handler uses Handler for inter-thread communication to achieve the purpose of updating UI by sub-threads

In the next period of time, we will explain in detail the method of using Handler for inter-thread communication, the principle of Handler, and the use of HanderTread and AsyncTask, etc. Please look forward to it~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643803&siteId=291194637