First, let’s understand the relationship between threads and processes
-
Process: can be understood as a running application, is the program is loaded into memory, the system allocates resources for it to run, and this execution of the program is called a process.
-
Thread: the basic execution unit of a process. A process has multiple threads. Multiple threads in the same process can execute simultaneously.
So why threads when you have processes? And what are the benefits of threading?
Processes have many advantages. They provide multiple programming channels, give us the feeling that we each own our own CPU and other resources, and increase the utilization of our computers. Many people don’t understand, if processes are so good, why bother with threads? In fact, there are some problems with it. Mainly in the following aspects:
- A process can only do one thing at a time, a process can only do one thing, and when it’s doing more than one thing, it can’t. This is where threads need to be used to meet this requirement.
- If a process blocks during execution, such as waiting for input, the entire process will be suspended, and even if some work in the process does not depend on the input data, it will not be able to execute.
From this we can see that a process that can only do one thing alone will be inefficient, as if we want to do one thing, we need assistance from many sides. Have listening, watching, action and other behaviors at the same time, so the efficiency will rise, and the solution is to thread to solve. A thread is like a carriage of a train, and a process is the train. A car (thread) cannot run without a train (process) that has at least one car (main train). Multithreading can be thought of as multiple carriages, and it appears to improve efficiency.
Because of concurrency, we invented processes, and then we invented threads. It’s just that processes and threads have different levels of concurrency: processes belong to the abstraction provided at the processor level; Threads, on the other hand, provide another level of concurrency abstraction at the process level. If we go into computer architecture, we see that the pipeline also provides concurrency, but at the instruction level. In this way, pipeline, thread, and process provide much needed concurrency at three levels from low to high!
In addition to increasing the concurrency of processes, threads also have the benefit of making efficient use of multiprocessor and multi-core computers. There is a tendency for processors to move toward multiple cores. Before threads, multiple cores did not make a process execute faster, because of all of the above two limitations. However, if a process is broken up into several threads, you can make the threads run on different cores, thus speeding up the process execution.
Multithreading and main thread
- Main thread: Handles the UI. All UI updates must be performed on the main thread. Do not put time-consuming operations on the main thread, will card the interface.
- Multithreading: A CPU can only handle one thread at a time, but the CPU can switch between multiple threads quickly enough to create the illusion that all threads are executing together.
- Multithreading improves the overall efficiency of the system by increasing resource utilization.
- Our purpose with multithreading is to put time-consuming operations in the background!
I recommend reading the next section on thread state and life cycle
This article refers to Wuhao, And Hazir
Next: Thread State and Life Cycle