Java multithreading learning (the summary is very detailed!!!)

Process: each process has an independent code and data space (process context). Switching between processes will have a large overhead. A process contains 1-N threads. (process is the smallest unit of resource allocation)

Threads: the same kind of threads share code and data space. Each thread has an independent running stack and program counter (PC), and the thread switching overhead is small. (threads are the smallest unit of CPU scheduling)

Create, ready, run, block, terminate.

One is to continue the thread class, and the other is to implement the runable interface (in fact, there should be three types. The other is to implement the callable interface and use it in combination with future and thread pool. This article will not talk about this.

1、 Extend Java Lang. thread class

The author recommends using runable, which will be explained later). Let's take a simple example

2、 Implement Java Lang. runnable interface

Summary:

Advantages of implementing runnable interface over inheriting thread class:

1) : it is suitable for multiple threads of the same program code to process the same resource

2) : you can avoid the limitation of singleton inheritance in Java

3) : increase the robustness of the program, the code can be shared by multiple threads, and the code and data are independent

4) : the thread pool can only put threads that implement runable or callable classes, not directly into classes that inherit threads

Let me remind you: the main method is actually a thread. In Java, all threads are started at the same time. When and which one executes first depends entirely on who gets the CPU resources first.

In Java, at least 2 threads are started each time the program runs. One is the main thread and the other is the garbage collection thread. Because whenever a class is executed with Java commands, a JVM is actually started, and each JVM starts a process in the operating system.

4、 Thread state transition

5、 Thread scheduling

Output result: < span style = "font family: 'Microsoft YaHei';" > Main the main thread starts running< span style="font-family: 'Microsoft YaHei';"> Main the main thread has finished running< span style="font-family: 'Microsoft YaHei';"> B thread running starts< span style="font-family: 'Microsoft YaHei';"> Sub thread B runs: 0 < span style = "font family: 'Microsoft YaHei';" > Thread a starts running< span style="font-family: 'Microsoft YaHei';"> Sub thread a runs: 0 < span style = "font family: 'Microsoft YaHei';" > Sub thread B runs: 1 < span style = "font family: 'Microsoft YaHei';" > Sub thread a runs: 1 < span style = "font family: 'Microsoft YaHei';" > Sub thread a runs: 2 < span style = "font family: 'Microsoft YaHei';" > Sub thread a runs: 3 < span style = "font family: 'Microsoft YaHei';" > Sub thread a runs: 4 < span style = "font family: 'Microsoft YaHei';" > End of thread a< span style="font-family: 'Microsoft YaHei';"> Sub thread B runs: 2 < span style = "font family: 'Microsoft YaHei';" > Sub thread B runs: 3 < span style = "font family: 'Microsoft YaHei';" > Sub thread B runs: 4 < span style = "font family: 'Microsoft YaHei';" > End of thread B< span style="font-family: 'Microsoft YaHei';"> It is found that the main thread ends earlier than the child thread < span style = "font family: 'Microsoft YaHei';" > Add join < div class = "DP highlighter bg_java" > < ol class = "dp-j" start = "1" > < Li class = "ALT" > < span class = "keyword" style = "font family: 'Microsoft YaHei';" > public class Main {

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>