Multithreading and threading

Multithreading

Multithreading means that there are multiple threads in a program, each thread performs an independent task, and multiple threads can execute concurrently

  • In Java, an application can contain multiple threads, each thread performs a specific task, and can be executed concurrently with other threads
  • Multi-threading minimizes the idle time of the system and improves CPU utilization. The multi-threaded programming environment hides the details of CPU switching between tasks with a convenient model
    • Throughput, make full use of cpu resources, reduce CPU idle time
    • Scalability, improve performance through the number of CPU cores

Thread

  • A thread is a smaller unit of execution than a process
  • Thread-lightweight process LWP, the system burden is small, mainly the allocation of CPU
  • Thread cannot exist independently, must exist in the process
  • Each thread also has its own process of birth, existence and death, which is also a dynamic concept
  • A thread has its own entry and exit, and a sequence of sequential execution

Working scenarios of threads:
1. Concurrent operations to avoid blocking and use resources more efficiently. Typical examples are: using worker threads in long-time working programs to avoid interface unresponsiveness. In the network download program, multiple threads are used to improve the efficiency of network usage and download files faster.
2. Parallel, thread is the smallest unit of processor scheduling. If your computer is equipped with multiple processors or cores, you can use multiple processors to calculate at the same time to speed up problem solving

Usefulness of threads

A process can be divided into multiple threads according to different functions. Each thread has its own stack segment and program counter. There is no independent data segment and code segment. The thread saves some local variables in its own stack segment. For some global Variables, all threads can share access to the data segment of the entire process

  • Multi-threading benefits: solve the problem of multiple parts running at the same time
  • Disadvantages of multi-threading: Too many threads will reduce efficiency. Generally, computationally intensive applications are not suitable for multi-threading.

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/113746089