Detailed explanation of Java multithreading (1) ------ introduction of the concept

This is the first chapter to explain Java multithreading. Before we enter the explanation, we need to understand the following concepts.

 

1. Concurrency and Parallelism

  Concurrent: means that two or more events occur at the same time (simultaneously);

  Concurrency: Refers to two or more events occurring within a time period.

  

  

  In an operating system, multiple programs are installed, and concurrency means that there are multiple programs running at the same time on a macro level within a period of time. In a single-CPU system, only one program can be executed at a time, that is, these programs are microscopically executed. It is time-sharing alternate operation, but it gives the impression that it is running at the same time, because the time-sharing alternate operation time is very short.

  In multiple CPU systems, these programs that can be executed concurrently can be allocated to multiple processors (CPUs) to achieve multi-task parallel execution, that is, each processor is used to process a program that can be executed concurrently, so that Multiple programs can be executed simultaneously.

  At present, the multi-core CPU in the computer market is a multi-core processor. The more cores, the more programs that can be processed in parallel, which can greatly improve the efficiency of computer operation.

 

Note: A computer with a single-core processor cannot process multiple tasks in parallel, only multiple tasks can run alternately on a single CPU.

 

 

2. Processes and threads

  Process: refers to an application program running in memory, each process has an independent memory space, and an application program can run multiple processes at the same time; a process is also an execution process of a program, and is the basic unit of a system running program; system Running a program is the process of creating, running, and dying of a process.

  Thread: An independent execution unit within a process; a process can run multiple threads concurrently, which can be understood as a process is equivalent to a single-CPU operating system, and threads are multiple tasks running in this system.

 

We can go to the taskbar at the bottom of the computer, right-click -----> open the task manager, you can view the process and thread of the current task

  

 

Thread options:

  

 

 

 3. The difference between process and thread

  Process: There is an independent memory space. The data storage space (heap space and stack space) in the process is independent, and there is at least one thread.

  Thread: Heap space is shared, stack space is independent, and threads consume much less resources than processes.

 

Note: 1. Because multiple threads in a process run concurrently, there is a sequence from a microscopic point of view. Which thread is executed depends entirely on the scheduling of the CPU, and the programmer cannot interfere. And this also causes the randomness of multi-threading.

   2. The process of a Java program contains at least two threads, the main process is the main() method thread, and the other is the garbage collection mechanism thread. Whenever a class is executed with the java command, a JVM is actually started. Each JVM actually starts a thread in the operating system. Java itself has a garbage collection mechanism, so at least two JVMs are started when Java is running. thread.

   3. Since the overhead of creating a thread is much smaller than that of creating a process, when we develop multitasking, we usually consider creating multithreading instead of creating multiprocessing.

 

 

4. Advantages of multithreading

  1. Processes cannot share memory, but threads can share memory.

  2. The system creation process needs to reallocate system resources for the process, and the cost of creating threads is much smaller. Therefore, when multitasking is concurrent, multithreading is efficient.

  3. The Java language itself has built-in support for multi-threading functions, rather than simply serving as a scheduling method for the underlying system, which simplifies multi-threaded programming.

 

Note: Multithreading is to complete multiple tasks synchronously, not to improve the efficiency of program operation, but to improve the efficiency of the system by improving the efficiency of resource usage.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580266&siteId=291194637