I. Processes and Threads
1. What is a process
A process is an application running in a system that is independent of each process and that each process runs within its dedicated and protected memory space
For example, we open the Mac QQ, player (fast broadcast you still dare to use it?) ), you can find out the detailed data of QQ and player in the activity Monitor of your Mac computer. In other words, there are already two software in the computer that are working.
2. What is a thread
Overall framework process: The function puts the task in the queue and then the thread executes the task in the queue.
1 processes to perform a task, a thread must be wired (at least 1 threads). A thread is the basic unit of execution for a process, and all the tasks of a process (program) are executed in threads. For example, using a cool dog to play music, using Thunder to download movies, all need to execute in the thread
So what thread is at least one thread? That is, our program handles the main thread of the event, it can also be said to refresh the UI threads,
Main thread: After an iOS program runs, the default is to turn on 1 threads, called the "main thread" or "UI thread"
3. Main function of primary thread
Display \ Refresh UI interface
Handling UI events (such as click events, scrolling events, drag events, etc.)
Use of 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 users a "card" bad experience
4. Thread's serial
The execution of a task in 1 threads is serial
If you want to perform multiple tasks in 1 threads, you can perform these tasks sequentially, one at a-
That is, at the same time, 1 threads can only perform one task
For example, the download of 3 files in 1 threads (file A, file B, file C, respectively) can only be downloaded one at a time, the CPU will deal with a thing at a time. For example, a worker working in a conveyor belt can only work when the first person has finished working on the transmission belt to the second worker.
5. Thread's serial
The execution of a task in 1 threads is parallel
If you want to perform multiple tasks in 1 threads, the task is executed at the same time.
That means that at the same time, 1 threads have to perform multiple tasks simultaneously
Or the above example, we want to download 3 files (respectively, file A, file B, file C), then in the parallel thread is to download the 3 files.
6. The principle of multithreading
At the same time, the CPU can only handle 1 threads, and only 1 threads are working (executing)
Multi-threaded concurrency (simultaneous) execution, in fact, the CPU is quickly dispatched between multiple threads (switching)
If the CPU schedules threads fast enough, it creates the illusion of multi-threaded concurrency execution
Think: What happens if the threads are very, very much?
CPU will be dispatched between N multithreading, the CPU will be exhausted, consumes a lot of CPU resources
The frequency at which each thread is scheduled to execute is reduced (thread execution efficiency is reduced)
7. Advantages and disadvantages of multithreading
Advantages of multithreading
To improve the execution efficiency of the program appropriately
Can appropriately improve resource utilization (CPU, memory utilization)
Disadvantages of multithreading
Open threads need to occupy a certain amount of memory (by default, the main thread takes up 1M, the child threads occupy 512KB), 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
Multi-threaded explanation