Processes and Threads
What is a process?
Proximity refers to an application that is running in the system.
Each proximity is independent, with each proximity running within its dedicated and protected memory space.
* * You can view all open processes on your Mac system through Activity Monitor.
What is a thread?
A process has to be wired (at least one thread per process) to perform a task.
A thread is the basic unit of execution for a process, and all the tasks of a process (program) are executed in threads.
thread's serial
The execution of a task in a thread is serial (executed sequentially)
If you want to perform multiple tasks in 1 threads, you can perform these tasks sequentially, one at a-to-one.
That is, at the same time, a thread can only perform one task.
Multithreading
1. What is multithreading?
Multiple threads can be opened in 1 processes, and each thread can perform different tasks in parallel (at the same time).
Multithreading technology can improve the execution efficiency of the program.
2. The principle of multithreading
At the same time, the CPU can only handle 1 threads, and only 1 threads are working (executing).
multithreaded concurrency (simultaneous) execution is the fact that the CPU is quickly dispatched (switched) between multiple threads.
If the CPU schedules the thread fast enough, it creates the illusion of multi-threaded concurrency execution.
3. Advantages and disadvantages of multithreading
Advantages: It can improve the execution efficiency of the program properly.
Can appropriately improve resource utilization (CPU, memory utilization).
Disadvantage: The open thread needs to occupy a certain amount of memory space (by default, the main thread takes up 1M, the child threads occupy 512K), if you open a large number of threads, will consume a lot of memory space, reduce the performance of the program.
The more threads, the greater the overhead of the CPU on the dispatch thread.
Programming is more complex: such as communication between threads, multi-threaded data sharing.
4. Multithreading in the development of iOS application
After an iOS program runs, the default is to turn on 1 threads, called the "main thread" or "UI thread."
5. Main thread function:
Displaying and refreshing the UI interface
Handle UI events (such as click events, scrolling events, drag events).
6. Use the main thread note:
Do not put more time-consuming operations into the main thread.
Time-consuming operations can get stuck in the main thread, seriously affecting the smoothness of the UI, giving the user a "card" bad experience.
Multi-threaded implementation scheme in 7.iOS
Basic concepts of multithreading in iOS