A, processes,
A process is an independent unit of system scheduling and resource allocation.
In Android, an application is a separate integration, and the application runs in a separate environment, avoiding interference from other applications/processes. When we start an application, the system creates a process (forked from Zygote with a separate ID), creates a main thread for the process, and then runs MainActivity. By default, the application’s components run in its process. Android :process = “process name “; This allows components to run in different processes. Having components running in separate processes has both advantages and disadvantages. Let’s go through them one by one.
Benefits: Each application (that is, each process) has a memory budget. The total memory used by all programs running in this process cannot exceed this value. Having components running in different processes allows the main process to have more space resources. Consider using multiple processes when your application is large and requires a lot of memory resources (i.e., when users complain about OutOfMemory).
Disadvantages: Each process has its own VM instance, so it is difficult to share some data between processes. Therefore, inter-process communication is required to realize data sharing.
Second, the thread
A thread is an entity of a process. It is the basic unit of CPU scheduling and dispatch. It is a smaller unit that can run independently.
In Android, threads can have several states: created, ready, running, blocked, and finished. The UI thread is running when the application has components running. By default, all application component actions are performed in the UI thread, including responses to user actions (touches, clicks, etc.), component lifecycle method calls, AND UI updates. Therefore, if the UI thread is in a blocked state (doing time-consuming operations in the thread, such as a network connection), it will not be able to respond to various operations. If the blocking time reaches 5 seconds, the application will be in ANR (Application Not Response) state.
1. Threading
Reduce the time and space cost of concurrent execution and improve the concurrent performance of the operating system.
2. Thread classification
Daemon threads, non-daemon threads (user threads)
2.1 Daemon Thread
Definition: a thread that guards the user thread, that is, provides a common service to other threads while the program is running
Common: such as garbage collection threads
Setting mode: thread.setdaemon (true); // Set this thread as a daemon thread
2.2 Non-daemon Threads (user threads)
Main thread & child thread.
2.2.1 Main Thread (UI Thread)
Definition: The Android system automatically starts a main thread when an application is started
Role: To deal with the interaction of the four components with the user (such as UI, interface interaction related)
Because the user interacts with the interface at any time, the main thread must be very responsive at all times, so the main thread is not allowed to perform time-consuming operations, otherwise ANR will occur.
2.2.2 Child Thread (Worker Thread)
Definition: manually created threads
Role: Time-consuming operations (network requests, I/O operations, etc.)
2.3 Differences and relationships between daemons and non-daemons
Difference: Whether the VM has exited, that is
A. When all user threads terminate, the daemon thread terminates and the virtual machine exits because there is no need for daemons
B. Conversely, as long as any user thread is running, the daemon thread does not terminate and the virtual machine does not exit
3. Thread priority
3.1 said
Thread priorities are divided into 10 levels, each represented by a Thread class constant.
Thread.MIN_PRIORITY // Priority 1 thread. MAX_PRIORITY // Priority 10Copy the code
3.2 set up
The setPriority(int grade) method is used to set the priority. The default Thread priority is 5, that is, thread.norm_priority.
4. Thread status
Creation state: when a thread is created using the new operator
Ready: When the start method is called, the thread in the ready state does not necessarily execute the run method immediately and needs to wait for the CPU to schedule it
Running status: THE CPU starts scheduling threads and executing the run method
Blocked (pending) : A thread is blocked during execution for some reason, such as calling sleep/wait, trying to obtain a lock, etc
End (die) state: The run method finishes execution or encounters an exception during execution
(1) The difference between start() and run()
Start a Thread that is ready and not running by calling the start() method of the Thread class. The method run() is called the Thread body. It contains the contents of the Thread to be executed. When run() completes, the Thread terminates, and the CPU schedules other threads.
(2) The difference between sleep(), wait() and yield()
The sleep() method belongs to Thread, and the wait() method belongs to Object.
When the sleep() method is called, the thread does not release the object lock, but suspends execution for a specified period of time. By calling wait(), the thread waivers the object lock and enters the wait lock pool waiting for the object. Without calling notify(), the thread is always ready (suspended).
Yield () jumps directly from the running state back to the ready state to yield the thread, the CPU, and the CPU scheduler to re-schedule. Comity may or may not work, that is, go back to the scheduler and play fair with the other threads.
The difference between a process and a thread
-
Thread is an entity of process, is the basic unit of CPU scheduling and dispatching, it is smaller than process can run independently of the basic unit; Process is an independent unit of system scheduling and resource allocation.
-
Threads do not own system resources; Processes can claim and own system resources;
-
Resource sharing between threads; Resources are shared between processes through IPC.
Four, multi-threading
1. Principles of Android threads
-
Cannot do time-consuming operations on main thread
-
You can no longer update the UI in a non-UI thread
(1) Why can’t we do time-consuming operations in the main thread
To prevent ANR from doing time-consuming operations on the UI main thread, we can put time-consuming operations on another worker thread. When the operation is complete, the UI main thread is notified to respond accordingly. This requires mastering how threads communicate with each other. Android provides two ways of communication between threads: AsyncTask mechanism and Handler mechanism.
(2) Why not update the UI in the non-UI thread? Android UI thread is not thread-safe, and UI update is implemented by calling the invalidate() method to redraw the interface, which means that when we update the UI in the non-UI thread, There may be other threads or UI threads that are also updating the UI, which can cause the interface updates to be out of sync. So we can’t update the UI in a non-UI main thread.
2.Android several ways to achieve multithreading
(1) implements Runnable (2) extends Thread (3) Handler.sendMessage () handler.post(Runnable action) RunOnUiThread (Runnable action) // Handler. post(4) AsyncTask (6) IntentService (7) ThreadPoolCopy the code
3. Why multithreading
The nature of multithreading is asynchronous processing, intuitive point is not to let the user feel “very card”.
4. What is at the heart of multithreading
The core mechanism of multithreading is Handler