Multithreading 01 --- multithreading basics, multithreading 01 --- Basics
1. Process and thread 1. Process
What is a process?
- A process is an application running in the system.
- Each process is independent, and each process runs in its dedicated and protected memory space.
For example, if both thunder and Xcode are enabled, the system starts two processes respectively.
Pass"Activity monitor"You can view the processes enabled in the Mac system.
2. threads
What is thread
- To execute a task, one process must have at least one thread (each process must have at least one thread)
- All tasks of a process (Program) are executed in the thread.
For example, playing music with cool dogs and downloading movies with thunder all need to be executed in the thread:
3. Thread serial
Task execution in one thread is serialized.
- If you want to execute multiple tasks in one thread, you can only execute these tasks one by one in order.
- That is to say, at the same time, one thread can only execute one task
For example, download three files (File A, file B, and file C) in one thread)
2. multithreading basic concepts Multithreading
What is multithreading?
- Multiple Threads can be enabled in one process, and multiple threads can execute different tasks in parallel (at the same time ).
- Process Workshop, thread workshop workers
- Multi-thread technology can improve program execution efficiency
For example, you can enable three threads to download three files (File A, file B, and file C) at the same time)
Multithreading Principle
- At the same time, the CPU can only process one thread, and only one thread is working (executed)
- Multi-thread concurrent (concurrent) execution is actually a fast CPU scheduling (switching) between multiple threads)
- If the CPU scheduling thread time is fast enough, it creates the illusion of multi-thread concurrent execution.
Think: what happens if there are many threads?
1. The CPU will be scheduled between N threads, and the CPU will be exhausted, consuming a lot of CPU resources
2. The frequency of scheduled execution of each thread is reduced (the execution efficiency of the thread is reduced)
Advantages and disadvantages of Multithreading
Advantages of Multithreading
- Appropriately improves program execution efficiency
- Resource utilization can be appropriately improved (CPU and memory usage)
Disadvantages of Multithreading
- There is an overhead for creating a thread. The main costs in iOS include: kernel data structure (about 1 kb), stack space (sub-thread kb, Master thread 1 MB, or-setStackSize: set, but must be a multiple of 4 K, and the minimum is 16 K). It takes about 90 milliseconds to create a thread.
- Enabling a large number of threads will reduce program performance.
- The more threads, the higher the CPU overhead on the scheduling thread.
- Program Design is more complex: for example, communication between threads and multi-thread data sharing
3. multi-threaded applications in iOS development
What is a main thread?
- After an iOS program runs, one thread is enabled by default, which is called "main thread" or "UI thread"
Main functions of the main thread
- Display \ refresh UI
- Process UI events (such as click events, rolling events, and drag-and-drop events)
Usage of the main thread
- Do not place time-consuming operations in the main thread
- Time-consuming operations will be stuck in the main thread, seriously affecting the smoothness of the UI, giving users a "card" bad experience
Time-consuming operations
If time-consuming operations are placed in the main thread
If time-consuming operations are placed in sub-threads (background threads, non-main threads)
4. Implementation of multithreading in iOS
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.