Joke Collection Website - Talk about mood - Why introduce threads?

Why introduce threads?

To talk about threads, we must first talk about processes, which are an example of program runtime. Threads can be regarded as occupying CPU time alone to execute the corresponding code. For early computers (such as DOS), a thread is both a process and a process, because she is single-threaded. Of course, a program can be multithreaded, and each thread of multithreading seems to complete its work in parallel, just like multiple processors running on a computer. When multithreading is implemented on a multiprocessor computer, they can really work in parallel, and the efficiency of program operation can be greatly improved by adopting appropriate time-sharing strategy. However, there is still a big difference between the two. Threads share the address space, which means that multiple threads can read the same address space at the same time and use this space to exchange data.

Anyone who has studied computer architecture knows that. Compared with multi-thread parallel execution, sequential execution of programs can greatly improve efficiency. For example, there are five threads Thread 1, Thread 2, Thread 3, Thread 4 and Thread 5, and the CPU time consumed is 4, 5, 1, 2 and 7 respectively. (Assuming that the CPU rotation cycle is 4 CPU times and the threads are independent of each other) Sequential execution takes 19 CPU time, while parallel execution takes less than 19 CPU time. As for the specific time, it depends on which threads can be executed simultaneously. This is in a very small-scale situation, and the efficiency can be higher if faced with the interaction between large-scale processes.