What is a process?
A process is an application that is running in the system. Each process is independent of each other and runs in its own dedicated and protected memory spaceCopy the code
What is a thread?
In order for a process to perform tasks, it must have threads (at least one thread per process) in which all tasks of a process (program) are executedCopy the code
Serial in threads
The execution of tasks in a thread is sequential. If multiple tasks are to be executed in a thread, they can only be executed sequentially. That is, a thread can only execute one task at a timeCopy the code
Process and thread comparison
Threads are the smallest units of CPU calls (mission) process is the unit of CPU resources and scheduling A program that can correspond to multiple processes, a process can have multiple threads, but at least one thread of threads within the same process the process of sharing resources, for example, the workshop is a process, so the workshop worker threadsCopy the code
multithreading
Multiple threads can be enabled in a process, and each thread can perform different tasks in parallelCopy the code
Principle of multithreading
The CPU can only handle one thread at a time, only one thread is executing a Task and multiple threads are executing concurrently, which means the CPU is scheduling between multiple threads very quickly and the CPU is scheduling threads fast enough to create the illusion of multiple threads executing concurrently if there are too many threads, The CPU is scheduled among N threads. As a result, the CPU is exhausted and consumes a large amount of CPU resources, reducing the running efficiency of threadsCopy the code
Advantages of multithreading
The CPU can only process one thread at a time. For example, if the CPU utilization is 20% for a thread, then multiple threads will use up all of the CPU utilization.Copy the code
Disadvantages of multithreading
There is overhead (space) when creating threads. IOS main costs: it takes about 90 milliseconds to create a thread, stack space (512KB child thread, 1MB main thread), kernel data structure (about 1KB). If a large number of threads are opened, the performance of the program will be reduced. The more threads, the greater the COST of CPU in scheduling threads (the program is slow, the operation is not smooth), making the program design more complex: For example, communication between threads. Data sharing between multiple threads.Copy the code