Differences between single-thread and multi-thread, single-thread, and multi-thread
Programs, processes, threads
Program: executable Application Generated by source code. (For example, QQ. app)
Process: A running program can be considered as a process (for example, a running QQ alarm once). The process has all the resources required for independent operation.
Thread: A code segment that runs independently in a program. (For example, the Code for receiving QQ messages)
A process is composed of one or more threads. A process is only responsible for resource scheduling and allocation. A thread is the real execution unit of the program and is responsible for code execution.
Single thread
Each running program (that is, a process) includes at least one thread, which is called the main thread.
The main thread is created when the program is started and used to execute the main function.
A program with only one main thread, called a single-threaded Program
The main thread is responsible for executing all the code of the Program (UI display and refresh, network requests, local storage, etc ). These codes can only be executed sequentially and cannot be executed concurrently.
Multithreading
A program with multiple threads is called a multi-threaded program.
IOS allows users to open up new threads themselves. These threads are called subthreads as opposed to the main threads.
Several sub-threads can be opened as needed
The sub-thread and main thread are independent operation units, and their respective operations do not affect each other. Therefore, they can be executed concurrently.
Differences between single thread and multi-thread
Single-threaded program: Only one thread executes the code in sequence and is prone to code blocking (the page is suspended)
Multi-threaded program: multiple threads run independently between threads, which can effectively avoid code blocking and improve program running performance.
Note: In iOS, you must add and refresh the UI in the main thread.
Multithreading on iOS platform
IOS multithreading implementation types
NSThread
NSOperationQueue
NSObject
GCD
NSThread
NSThread is a lightweight multi-threaded program. It can be created using either of the following methods:
Note:
In the multi-threaded method, you need to add an automatic release pool.
When an application is opened, the system automatically creates an automatic release pool for the main thread.
The sub-thread we manually created requires us to manually add the Auto Release pool.