内容简介:创建Thread对象时this.getName()和Thread.currentThread().getName()的差异
非构造参数创建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()永远是一样的,获取当前运行线程的名称。
以上所述就是小编给大家介绍的《创建Thread对象时this.getName()和Thread.currentThread().getName()的差异》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
程序员代码面试指南:IT名企算法与数据结构题目最优解
左程云 / 电子工业出版社 / 2015-9 / 79.00元
这是一本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一痛点,本书选取将近200道真实出现过的经典代码面试题,帮助广大程序员的面试准备做到万无一失。“刷”完本书后,你就是“题王”!__eol__本书采用题目+解答的方式组织内容,并把面试题类型相近或者解法相近的题目尽量放在一起,读者在学习本书时很容易看出面试题解法之间的联......一起来看看 《程序员代码面试指南:IT名企算法与数据结构题目最优解》 这本书的介绍吧!