Home >Java >javaTutorial >Java thread group construction method source code analysis

Java thread group construction method source code analysis

王林
王林forward
2023-04-22 22:55:15679browse

Thread group construction method

Let’s look at this thread group. The name of the thread group is system, set the priority, and then specify that the parent thread is empty. You can see that this is the root thread group, and this method is private. , is not called for us.

Java thread group construction method source code analysis

Add the current thread to the thread group

Java thread group construction method source code analysis

Let’s try the above code logic, yes, no thread is specified The group will join the thread group of the currently created main thread. If the thread group is specified, it will be the current thread group.

Java thread group construction method source code analysis

The same is true when we look at the source code:

Java thread group construction method source code analysis

The number of active threads and active thread groups is both 0:

Java thread group construction method source code analysis

We have not opened the thread, so we need to open the thread. Here you can see the number of active threads:

Java thread group construction method source code analysis

is Why start the thread and you can count it?

Let's take a look at the source code. Here is the thread added to the thread group after it is started:

Java thread group construction method source code analysis

Then the thread is started to add and add, and the thread that is not started is subtracted.

Java thread group construction method source code analysis

Can there be a thread group under a thread group?

Let’s experiment. We just added a sub-thread group, as follows:

Java thread group construction method source code analysis

At this time we can see that there can be another thread group under it Thread group

Java thread group construction method source code analysis

#That is to say, there can be threads or thread groups under the thread group.

Let’s take a look at the thread group of the main thread. The code is as follows:

Java thread group construction method source code analysis

#The thread group of the main thread is still main, and the parent thread group of the main thread is us When talking about system, this root thread group can have sub-threads and sub-thread groups below it, and so on.

The above is the detailed content of Java thread group construction method source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete