Java multithreading learning road (1)-introduction to common concepts

Java multithreading learning road (1)-introduction to common concepts

1. Common concepts of Java multithreading

1.1. What is a thread ( Thread )

A thread is an execution unit in a process. It is the smallest unit that can be scheduled in the operating system. The thread basically does not occupy system resources. It only includes the thread ID, program counter, register group and stack, and shares code with other threads belonging to the same process. Segments, data segments and other resources

1.2. What is a process ( progress )

A process is a running instance of an independent program. A program can have multiple corresponding processes, and each process has an independent space.
For example, if a QQ is downloaded and installed in the computer, then this QQ is a program.
Log in to a QQ account, then This running QQ is a process, and then log in to another QQ account, then it is another process.

1.3. The relationship between process and thread

A process in modern computers usually has multiple threads, and a thread belongs to only one process. All threads in a process share resources

1.4. What is the meaning of multithreading

  • On a single-core processor , the performance of the program is improved and the user experience is improved. For example, a program usually needs to perform I/O operations, and if it is a single-threaded program, I/O reading is relatively slow and may be blocked . You can only wait, which greatly affects the user experience.
  • On a multi-core processor , the utilization and throughput of the CPU are improved, and the user experience can also be improved. Modern processors are basically multi-core and multi-threaded. For example, the AMD R7-4800H of my Yoga 14s is 8 cores and 16 threads. .Using multi-threaded programs can make use of the CPU performance as much as possible.

1.5 Thread Classification

Threads in java are divided into two categories:

  • User thread ( User ) : Creating a thread is the user process by default, the main() function will execute the Main Thread, and MainThread is also a user thread
  • Daemon (Background) Thread ( Daemon ) : Daemon is a thread that provides services in the background while the program is running. It is not indispensable. The user can call the setDaemon() method before the user thread run() It is set to Daemon.

1.6 Four advantages of multi-threaded programming (operating system concept)

  1. Responsiveness : Current programs are basically interactive. If multi-threaded programming is used, even if it is partially blocked or performs lengthy operations, it can still continue to execute, thereby increasing the degree of responsiveness to users.
  2. Resource sharing : Because processes are not in the same memory space, resource sharing will become relatively difficult, and resources can only be shared through technologies such as shared memory and message passing. However, threads of the same process are all in the same memory space , So there is no such difficulty.
  3. Economy : A process can be said to be a big man. For example, the IntelliJ IDEA I am running now occupies nearly 900M of memory space. Compared with the process, the thread can be said to be short and compact, and the overhead of creating and switching threads is much smaller than the process .
  4. Scalability : For multi-processor architectures, the advantages of multi-threading are greater because threads can run in parallel on multiple processor cores .

1.7 parallelism ( Parallelism ) and concurrent ( Concurrency )

1.7.1 Parallel

Parallelism is because a multi-core computer has multiple processing cores.For a multi-threaded program, two cores can process different threads at the same time, just like there are two lanes on the road, and the car can drive in parallel on two lanes.

Insert picture description here

1.7.2 Concurrency

Concurrency means that in a period of time, there are several programs that are in between starting and running, and these programs are all running on the same processor, but only one program is running on the processor at any one time.
Insert picture description here

1.8 Asynchronous threads and synchronous threads

  • Asynchronous thread

  • Synchronization thread

1.9 thread status

Threads have different states. A thread can only have one state at a time. Many blogs on the Internet say that there are 5 kinds (maybe 5 kinds in the operating system), and there are 6 kinds of states for java threads (you can see in the jdk source code) To). They are:

  • New (initial state)
  • Runnable (running state)
  • Blocked (blocked state)
  • Waiting (waiting state)
  • Timed_Waiting (Waiting overtime)
  • Terminated (terminal state)

1.10 Thread priority and thread scheduling

Thread scheduling in Java is preemptive , and Java threads have different priorities. Threads with higher priority are executed first, but this is not necessarily true. It is just that threads with higher priority are more likely to execute first. High .

1.11 thread pool ( the Thread Pool )

1.12 Lock ( Lock )

A thread with a higher priority is more likely to execute first .

1.11 thread pool ( the Thread Pool )

1.12 Lock ( Lock )

The lock is to solve the error when multiple threads access the same resource. Just like its name, when a thread accesses the resource, a lock is added to it, thereby preventing other threads from accessing it.

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109440577