The difference between this.getName() and Thread.currentThread().getName() when creating a Thread object

Creating Thread objects with non-constructive parameters

First, look at the following code:

class NewThread extends Thread{

    public NewThread() {
        // TODO Auto-generated constructor stub
        System.out.println("------NewThread  stard-------");
        System.out.println("Thread.currentThread().getName()>>>"+Thread.currentThread().getName());
        System.out.println("this.getName()>>>"+this.getName());
        System.out.println("NewThread.currentThread.getName()>>>"+NewThread.currentThread().getName());
        System.out.println("------NewThread  end-------");
    }
    @Override
    public void run() {
        System.out.println("------run  stard-------");
        System.out.println("Thread.currentThread().getName()>>>"+Thread.currentThread().getName());
        System.out.println("this.getName()>>>"+this.getName());
        System.out.println("NewThread.currentThread.getName()>>>"+NewThread.currentThread().getName());
        System.out.println("------run  stard-------");
    }
}

public class test { 
    public static void main(String[] args) {
        NewThread thread1 = new NewThread();
        thread1.setName("A");
        thread1.start();
    }

}

Run the above code and print out:
——NewThread stard——-
Thread.currentThread().getName()>>>main
this.getName()>>>Thread-0
NewThread.getName()>>>main
——NewThread end——-
——run stard——-
Thread.currentThread().getName()>>>A
this.getName()>>>A
NewThread.getName()>>>A
——run stard——-
Thread. current Thread (). getName () and using this.getName() and NewThread. current Thread. getName () can get the name of the thread. We find that Thread. current Thread (). getName () and NewThread. current Thread. getName () in the construction method get the main thread, indicating that the current thread executing the construction method of NewThread() is the main thread; this.getName() gets Thread-0. Why Thread-0?
Let's look at the parent Thread source of NewThread:

In the new NewThread(), the construction method of the parent class is executed according to the execution order of the parent and child classes. In this case, the name of the Thread class has been initialized with "Thread-"+nextThreadNum(), so this.getName() gets Thread-0.
In run's method, getName() is all A, thread1.start() indicates that thread1 thread starts thread, Thread. current Thread (). getName() and NewThread.getName() get A, and because after NewThread(), thread1.start() already got thread1.setName("A"), so this.getName() gets A.

Construct parameters to create Thread objects

class NewThread extends Thread{

    public NewThread() {
        // TODO Auto-generated constructor stub
        System.out.println("------NewThread  stard-------");
        System.out.println("Thread.currentThread().getName()>>>"+Thread.currentThread().getName());
        System.out.println("this.getName()>>>"+this.getName());
        System.out.println("NewThread.currentThread.getName()>>>"+NewThread.currentThread().getName());
        System.out.println("------NewThread  end-------");
    }
    @Override
    public void run() {
        System.out.println("------run  stard-------");
        System.out.println("Thread.currentThread().getName()>>>"+Thread.currentThread().getName());
        System.out.println("this.getName()>>>"+this.getName());
        System.out.println("NewThread.currentThread.getName()>>>"+NewThread.currentThread().getName());
        System.out.println("------run  stard-------");
    }
}

public class test { 
    public static void main(String[] args) {
        Thread thread2 = new Thread(new NewThread(),"B");
        //Amount to
        //NewThread newThread = new NewThread();
        //Thread thread2 = new Thread(newThread ,"B");
        thread2.start();
    }

}

This part of the code is different from the main method in test in creating Thread objects with non-constructive parameters. The other part is the same. Run print output:
——NewThread stard——-
Thread.currentThread().getName()>>>main
this.getName()>>>Thread-0
NewThread.currentThread.getName()>>>main
——NewThread end——-
——run stard——-
Thread.currentThread().getName()>>>B
this.getName()>>>Thread-0
NewThread.currentThread.getName()>>>B
——run stard——-
It is found that the result of creating Thread objects with non-constructive parameters is the same. Only this.getName() in run method can get Thread-0. Because the construction method is the same, so the printing result is the same in the construction method. Let's look at the source code again.

Look at the source comment, target: When this thread starts, select the run method of the object to run, and name is the name of the new thread. (Let's call new New Thread () as new Thread) So when thread2.start(), it's actually the run method of the new Thread called, but the name of the new Thread is not assigned, and its name is still the assignment of its parametric construction, that is, the following figure.

So at this point, this.getName() is just a default value, Thread-0. Thread. current Thread (). getName () and NewThread.getName() get B because they get the name of the current running thread, so this.getName() gets B.

summary

To sum up, this.getName() here is quite different from (Thread.currentThread().getName() and NewThread.currentThread. getName(). this.getName() just gets the name of the parent class object, just a method call; whereas Thread.currentThread and NewThread.currentThread are actually the same. NewThread inherits Thread, and all can be called. EAD is the native Method, so their getName() is always the same. Get the name of the current running thread.

Posted by johlwiler on Thu, 27 Dec 2018 16:03:06 -0800