This blog cat uncle’s blog, reprint please state source

Read this article in 4 minutes

Suitable audience: Java beginners

Threads are everywhere.

Let’s start with a few basic concepts.

A process can contain multiple threads, and the threads in the same process share the resources that the process has applied for, such as memory space and file handles.

From the JVM’s perspective, a thread is a Component of a process.

Any piece of code in a Java program always executes on a certain thread

Java threads are divided into Daemon threads and User threads.

User threads: All user threads in the application must be stopped before the JVM can stop properly, otherwise the JVM cannot stop

Daemon threads: Do not interfere with the JVM’s proper shutdown and usually perform low-priority tasks, such as monitoring the health of other threads

In multi-threaded operation, we need to pay attention to which thread is responsible for the execution of each section of code, which is related to performance problems, thread safety.

System.out.println("The ** method was executed by thread: " + Thread.currentThread().getName());
Copy the code

You can see which thread is responsible for executing the method above, but of course you can test your guess by creating a new thread that is responsible for executing the method.

Public number: Java cat said

Learning exchange Group: 728698035

Now architecture designer (code farmer) and entrepreneurial technology consultant, unruly mediocre, love open source, talk about program life and irregular dry goods.