1, the basic concept: 1-1, the process:
- Definition: A process is an application that is running in a system.
- Understanding: Each process is independent, with each process running in its dedicated and protected memory space.
1-2. Threads:
- To perform a task, a process must have a thread (that is, each process has at least one thread, which is the main thread).
- All tasks in a process are executed in the thread.
- A thread is the basic execution unit of a process.
1-3, Thread of serial:
- All tasks in a thread are executed serially, i.e., if multiple tasks are to be performed in 1 threads, more than one task can be executed sequentially, and 1 threads can only perform a single task at the same time.
1-4. The difference between a process and a thread:
?了解: The ① thread is the smallest unit of CPU execution tasks;
?了解: ② process is the basic unit of CPU allocating resources and dispatching;
?了解: ③ can have multiple threads in a process, and a process has at least one thread (main thread);
?了解: ④ a thread in the same process to share the resources of the process.
2, Multithreading: 2-1, Multithreading Introduction:
?重要:1, a process can open multiple threads, each thread can perform different tasks in parallel.
?重要:2, multithreading technology can improve the execution efficiency of the program to a certain extent.
as shown in the following:
2-2, Multithreading principle:
?重要:1, at the same time, the CPU can only handle one thread, only one thread is working (execution);
?重要:2, multi-threaded concurrent execution, the essence of the CPU is fast in the scheduling between multiple threads, switching execution, that is: If the CPU Scheduler thread fast enough, it can cause multi-threaded concurrent execution of the illusion.
?备注:Think: What happens if you have too many threads?
答案: ①CPU busy scheduling, switching between n threads, consuming a lot of CPU resources, ② the frequency at which each thread is scheduled to execute is reduced (that is, the execution of the thread is less efficient).
2-3, multithreading advantages and disadvantages: 1, multi-Threading benefits:
?重要:① can improve the execution efficiency of the procedure properly;
?重要:② can appropriately improve resource utilization (CPU, memory utilization).
2. Multithreading Disadvantages:
?重要:① the cost of creating a thread, the main costs under iOS include: Kernel data structure (approximately 1KB), stack space (sub-thread 512KB, main thread 1MB, can also use-setstacksize: set, but must be a multiple of 4KB, and the minimum is 16KB), Creating a thread takes approximately 90 milliseconds to create the time;
Line Stacks Space:
To set the line stacks space:
?重要:② If a large number of threads are turned on, the performance of the program is reduced, and as the thread increases, the overhead of the CPU on the dispatch thread increases.
?重要:The more ③ threads, the more complex the programming (for example: communication between threads, data sharing among multiple threads).
3, the main thread: 3-1, the main thread introduction:
?了解:After an iOS program runs, 1 threads are turned on by default, which is called the "main thread" or the UI thread.
3-2. Main thread:
?重要:1, display, refresh UI interface;
?重要:2. Handle UI events (for example: Click events, scrolling events, drag events, etc.).
3-3, the main thread of use note:
?重要:1, do not put the time-consuming operation in the main thread to execute;
?重要:2, time-consuming operation will be stuck in the main thread, seriously affect the smoothness of UI refresh, user experience is very poor.
3-4, how to determine whether the thread is the main path:
?了解:① View the number = = 1 of the thread or name = = Main;
?了解:Ismainthread method of ②nsthread class method;
?了解:③nsthread Object method Ismainthread method.
4. Multi-threaded implementation in iOS:
iOS Core notes-multithreading-basic knowledge