Thread.currentThread() does not return the thread in which the current program is running, but returns the thread in which the code Thread.currentThread() is executed.
So you can see that when the new thread is constructed, the code is executed in the main thread, while the code in the run method is executed in the thread.
1. new一个Thread对象的时候默认的名字就是Thread-n格式的,你可以看看Thread源码。 2、你这就是一个线程对象,this在你这样使用的情况下,是当前的线程了。 3、Thread.currentThread()Always returns the currently running thread.
Thread.currentThread() does not return the thread in which the current program is running, but returns the thread in which the code Thread.currentThread() is executed.
So you can see that when the new thread is constructed, the code is executed in the main thread, while the code in the run method is executed in the thread.
It’s easy to understand by looking at the output
1.
new
一个Thread
对象的时候默认的名字就是Thread-n
格式的,你可以看看Thread
源码。2、你这就是一个线程对象,
this
在你这样使用的情况下,是当前的线程了。3、
Thread.currentThread()
Always returns the currently running thread.The running result of the constructor method is understandable, but why is the running result of the run method not testThread in setName but thread?
1. The getName() method is inherited from the Thread class. Just look at the getName() method of the Thread class:
2.Yes
3.Yes
In the constructor, Thread.currentThread() is the main thread, and Thread.currentThread() in the run method is the current thread