1. Preface
Recently, I was interviewing and reviewed multi-threading again, hoping to deepen my understanding of multi-threading.
1. What is a process?
1). To understand threads, we must first understand the process. In layman's terms, a process is an application running in the system.
2). Each thread exists independently and runs in its dedicated and protected memory space.
3). For example, when opening the QQ or Xcode system, two processes will be opened separately as shown in the figure:
4) We can view the processes enabled in the Mac system through the "Activity Monitor".
2. What is a thread?
1). A process must have threads to execute tasks, that is, a process must have at least one thread.
2). Threads are the basic execution unit of a process, and all tasks of a process (program) are executed in the thread.
3). For example, using Kugou to play music and using Thunder to download movies all require running in the thread as shown in the figure:
3. What is thread serial?
1). A thread is executed serially (sequentially executed), which means that a thread can only execute one task within the same time.
2). Serial execution diagram, such as one thread downloads 3 files (files A, B, C)
4. What is multi-threading?
1). Multiple threads can be opened in a process, and each thread can perform different tasks concurrently (simultaneously).
2).Similar relationship list: Process---->Workshop; Thread---->Workshop workers
3). Multi-threaded diagram, such as opening 3 threads at the same time to download 3 files (files A, B, C)
5. Multithreading principle
1). At the same time, the CPU can only execute one thread, and only one thread is working (execution).
2). Multi-threaded concurrent (simultaneous) execution is actually the CPU quickly schedules (switches) between multiple threads.
3). If the CPU schedules threads quickly enough, it will cause the illusion of multi-thread concurrent execution.
4). Disadvantages of multithreading:
1. Each thread will occupy a certain amount of memory space (by default: the main thread occupies 1MB and the child thread occupies 512KB).
If too many threads are turned on, it will occupy a lot of memory space, which will cause program performance to be degraded.
2. The more threads, the greater the overhead of the CPU scheduling thread (similar to the more factory workers, the greater the factory overhead).
3. Make programming more complex: such as multi-threaded data communication and data sharing between multiple threads.
5). Advantages of multithreading:
1. Can appropriately improve the execution efficiency of the program.
2. Can appropriately improve resource utilization (CPU and memory utilization)
6. What is the main thread?
1).A thread will be opened by default after an iOS program is opened. This thread is called the "main thread" or "UI thread".
2). The main functions of the main thread:
1. Display/refresh the UI interface
2. Handle UI events (such as click events, scroll events, drag events, etc.)
3). Notes on the main thread:
1. Don’t place time-consuming operations in the main thread. Time-consuming operations in the main thread will cause program lag.
7. Time-consuming operation demo
1) Demo running directly in the main thread
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//Get the current execution method and the current thread//number== Main thread//number!= Other threads, child threads, and secondary threads NSLog(@"%s----%@",__func__,[NSThread currentThread]);//Operation directly in the main thread causes UI operation to stutter [self longTimeOperation];}#pragma mark-Time-consuming operation-(void)longTimeOperation{for (int i=; i<; i++) {NSLog(@"%d",i);}}2) Demo running in child thread
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//Get the current execution method and the current thread//number== Main thread//number!= Other threads, child threads, and secondary threads NSLog(@"%s----%@",__func__,[NSThread currentThread]);//Put the time-consuming operation in the child thread to execute without affecting the UI operation[self performSelectorInBackground:@selector(longTimeOperation) withObject:nil];}#pragma mark-Time-consuming operation-(void)longTimeOperation{for (int i=; i<; i++) {NSLog(@"%d",i);}}The above content is the multi-threading knowledge of ios introduced to you by the editor. I hope it will be helpful to you!