Thread.currentThread().getName() and this Getname() difference explanation

Posted by kb0000 on Sun, 20 Feb 2022 17:16:21 +0100

Python wechat ordering applet course video

https://edu.csdn.net/course/detail/36074

Python actual combat quantitative transaction financial management system

https://edu.csdn.net/course/detail/35475

Detailed explanation of currentThread

currentThread method is a static method of Thread class, which is used to obtain the currently running code segment and which Thread is calling. Let's take a look at the source code first.

Is a native method. Interact directly with the system level.
Let's look at a piece of code

    public static void main(String[] args) {
        String name = Thread.currentThread().getName();
        System.out.println(name);
    }

The output is main.

Why main?

When a java project is started, it will create a process, which is also a thread. In Java, it is called the main thread. His name was set as main. We can see that the above code is executed under the main method, that is, by the main thread, so the printed name is main.

Create a new thread

Looking at the following line of code, we create a new thread and print the name of the current execution thread in the construction method created by the thread and the run method actually executed by the thread.

public class TestThread extends Thread {

    public TestThread() {
        System.out.println("Construction method:" + Thread.currentThread().getName());
    }

    @Override
    public void run() {
        System.out.println("run method:" + Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.start();
    }

}

Let's run it and see the result

Construction method: main
run method: Thread-0

How to understand the above situation?

Let's start with the construction method line.

TestThread testThread = new TestThread();

Here we just declare a thread object class. This new thread is not created or started. We just understand it as an ordinary object. Since it is in the main method, it must be the main thread that executes it, so you can see that the output of the construction method is main.
Let's look at the start method. Why does it become thread-0.
We know that one way to create multithreads in java is to inherit the thread class. Then implement the run method inside. In this way, when the thread starts, it will call the internal local method of start0, which is actually the implementation method of run. When the run method is executed, it must be executed by the thread we created, not the main thread, so we can know that the name of the new thread is thread0.

Why is the name of the new thread thread0?

We create threads by inheriting the thread class. When we construct, we will execute the construction method of the parent class. We are a null parameter construct, so we need to see what the null parameter construct of the parent class is like. Look at the source code

You can see that there is a name parameter, which has spliced a thread for us - followed by a variable. Let's continue to see what this variable is

A synchronous static method, and then look at the value of the variable.

A static int variable, so we know that the initial value of int is 0. We first get 0 and then execute + +. When other threads initialize again, they start from 1. Because of the keyword of synchronization lock, we don't have to be afraid that the data will be disordered.
So we know that the name of the new thread is the source of thread-0.

Look at another case with this

public class TestThread extends Thread {

    public TestThread() {
        System.out.println("Construction method start!");
        System.out.println("Construction method:" + Thread.currentThread().getName());
        System.out.println("Construction method this name:" + this.getName());
        System.out.println("End of construction method!");
    }

    @Override
    public void run() {
        System.out.println("run Method start!");
        System.out.println("run method:" + Thread.currentThread().getName());
        System.out.println("run Methodical this name:" + this.getName());
        System.out.println("run End of method!");
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.start();
    }

}

The results of the implementation are as follows:

Construction method start!
Construction method: main
 Construction method this name: Thread-0
 End of construction method!
run Method start!
run method: Thread-0
run Methodical this name: Thread-0
run End of method!

We found that the name of this constructed is thread-0. This is not difficult to understand. This refers to the name of the current object. Because our thread has a name during initialization, it is thread-0.

setName of the created thread

public class TestThread extends Thread {

    public TestThread() {
        System.out.println("Construction method start!");
        System.out.println("Construction method:" + Thread.currentThread().getName());
        System.out.println("Construction method this name:" + this.getName());
        System.out.println("End of construction method!");
    }

    @Override
    public void run() {
        System.out.println("run Method start!");
        System.out.println("run method:" + Thread.currentThread().getName());
        System.out.println("run Methodical this name:" + this.getName());
        System.out.println("run End of method!");
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.setName("test");
        testThread.start();
    }

}

Take a look at the results of the implementation

Construction method start!
Construction method: main
 Construction method this name: Thread-0
 End of construction method!
run Method start!
run method: test
run Methodical this name: test
run End of method!

Someone here may have to ask why the name of this constructor is thread-0. The actual run time becomes test.
This is because when we construct the object, the object is not complete and the setName step is not executed. So the two are inconsistent.

Finally, look at a piece of complex code

public class TestThread extends Thread {

    public TestThread() {
        System.out.println("Construction method start!");
        System.out.println("Construction method:" + Thread.currentThread().getName());
        System.out.println("Construction method this name:" + this.getName());
        System.out.println("End of construction method!");
    }

    @Override
    public void run() {
        System.out.println("run Method start!");
        System.out.println("run method:" + Thread.currentThread().getName());
        System.out.println("run Methodical this name:" + this.getName());
        System.out.println("run End of method!");
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        Thread thread = new Thread(testThread);
        System.out.println("New thread thread Name of:" + thread.getName());
        thread.setName("test");
        thread.start();
    }

}

You can see the following results

Construction method start!
Construction method: main
 Construction method this name: Thread-0
 End of construction method!
New thread thread Name of: Thread-1
run Method start!
run method: test
run Methodical this name: Thread-0
run End of method!

According to the results, the printing of the construction method is the same as that explained above. Let's focus on the printing of new threads. Why is 1 here? It's 1 because we have + + after initializing 0 above.
The run method is test because the thread we actually execute thread = new thread (testthread); So the name we get is thread and the name is test.
So why is this name of the run method thread-0. Because this Getname gets the name of the current object. Our current object is TestThread. His thread name has never been changed, so we got thread-0.

summary

Getname of currentthread represents the name of the current executing thread, this Getname represents the name of the object. this.getName() actually returns target Getname(), while thread currentThread(). Getname () is actually thead getName().

Topics: Java Android Back-end computer