非构造参数创建Thread对象
首先看下面一段代码:
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();
}
}
运行上面代码,打印输出:
——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.currentThread().getName()和使用this.getName()和NewThread.currentThread.getName()都可以得到线程的名称,我们发现构造方法中Thread.currentThread().getName()和NewThread.currentThread.getName()得到的是main,说明执行NewThread()构造方法的当前线程为main主线程;this.getName()得到的是Thread-0 ,为什么是Thread-0?
我们看NewThread的父类Thread源码:
在new NewThread()的时候,按照父类子类执行顺序,先执行父类的构造方法,在这个已经给Thread类的name初始化”Thread-” + nextThreadNum(),所以这时候;this.getName()得到的是Thread-0。
在run的方法中,getName()全部为A,thread1.start()说明thread1 线程启动线程,Thread.currentThread().getName()和NewThread.getName()得到的是A,又因为在NewThread()后,thread1.start()之前已经thread1.setName(“A”),所以this.getName()得到的是A。
构造参数创建Thread对象
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");
//相当于
//NewThread newThread = new NewThread();
//Thread thread2 = new Thread(newThread ,"B");
thread2.start();
}
}
这部分代码与非构造参数创建Thread对象中代码只有test中的main方法不同,其他一致,运行打印输出:
——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——-
发现其地方与非构造参数创建Thread对象运行结果一致,只有在run方法中this.getName()得到的为Thread-0。 因为构造方法一致,所以构造方法中打印结果一致,我们再来看看源码
看源码注释,target:当这个线程开始时选择object的run方法运行,name是新线程的名字,(我们吧 new NewThread()当做newThread )所以在thread2.start()时,其实是调用的newThread 的run方法,但是newThread 的name却没有赋值,其名称还是他的无参构造赋值的,即下图。
所以此时this.getName()只是一个默认值Thread-0。而Thread.currentThread().getName()和NewThread.getName()得到的是B,因为他们获取的是当前运行线程的名称,所以this.getName()得到的是B。
总结
总结一下,其实这里的this.getName()与(Thread.currentThread().getName()和NewThread.currentThread.getName())有着很大区别,this.getName()只是拿到父类对象的名称,仅仅是一个的方法的调用;而Thread.currentThread和NewThread.currentThread其实是一样的,NewThread继承了Thread,所有可以调用currentThread这个native Method,所以他们的getName()永远是一样的,获取当前运行线程的名称。