Thread.currentThread().getName() 和 this.getName()详解
来源:互联网 发布:pic单片机指令集 编辑:程序博客网 时间:2024/05/21 10:22
首先,Thread.currentThread().getName() 和 this.getName()都可以用来获得线程的名称,但是它们是有区别滴,不能乱用!
下面分别对这两个方法进行剖析:
先来说说currentThread()方法,它的源码如下,它是一个本地方法,方法返回一个Thread对象:
/** * Returns a reference to the currently executing thread object. * * @return the currently executing thread. */ public static native Thread currentThread();
接下来看看 getName()方法,它是Thread类中的一个方法(并不是Object类中的方法),源码如下:
/** * Returns this thread's name. * * @return this thread's name. * @see #setName(String) */ public final String getName() { return String.valueOf(name); }
其中,name是Thread类中的一个域,是char[],getName()方法可以返回当前线程的名称。
所以我们可以总结:Thread.currentThread().getName()可以在任何情况下使用;而this.getName()必须在Thread类的子类中使用,this指代的必须是Thread对象!
为了更好的说明两者的差别,可以看以下代码:
public class MyThread extends Thread{ public MyThread() { System.out.println(".....MyThread begin....."); System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName()); System.out.println("this.getName() = "+this.getName()); System.out.println(".....MyThread end......."); System.out.println(); } @Override public void run() { System.out.println(".....run begin....."); System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName()); System.out.println("this.getName() = "+this.getName()); System.out.println(".....run end......."); }}
public class MyThreadTest { public static void main(String[] args) { // TODO Auto-generated method stub MyThread mt = new MyThread(); Thread t = new Thread(mt); t.setName("A"); t.start(); }}
输出结果为:
其中,Thread.currentThread().getName()调用的是执行此行代码的线程的getName方法;this.getName()调用的是mt对象的getName方法。区别显著有木有!!!
这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么鬼???
通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:
"Thread-" + nextThreadNum()
有兴趣的同学可以自己去查源码!
这就解释了为什么“this.getName() = Thread-0”。
3 0
- Thread.currentThread().getName() 和 this.getName()详解
- Thread.currentThread().getName() 和 this.getName()区别详解
- 多线程this.getName()和Thread.currentThread().getName()
- 多线程中this.getName()和Thread.currentThread().getName()
- 并发this.getName()和Thread.currentThread().getName()区别
- Thread.CurrentThread.getName 与 this.getName()
- currentThread().getName和this.getName()的区别
- Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
- 多线程当中this.name和Thread.currentThread.getName的区别
- Thread.currentThread().getName()与this.getName()的区别
- JAVA 多线程 this.getName()与Thread.currentThread().getName()
- 有关多线程中this.getName()和Thread.currentThread().getName()返回名字不一样的问题
- 多线程中this.getName()和Thread.currentThread().getName()返回名字不一样的问题
- 【java】多线程中Thread.currentThread().getName()和this.getName()的区别
- Java多线程中this.getName()和Thread.currentThread.getName()的区别?
- Thread.currentThread().getName()…
- Thread.currentThread().getName()
- 多线程的this.getName()与Thread.currentThread().getName为何有时不一样
- BJFU 1406
- 提高C++程序运行效率的10个简单方法
- 无限图片轮播器
- tempgauge测试-MQTT Over WebSocket
- Linux的学习之简单的指令一
- Thread.currentThread().getName() 和 this.getName()详解
- Java多线程、线程的生命周期和状态控制
- java Class.forname与SelectMethod=Cursor
- Spring mvc Controller层与前端JSON数据交互
- JDK环境变量配置
- android BSP与硬件相关子系统读书笔记(1)android BSP移植综述
- thinkphp 3.2 =》0625-6_登录业务与 验证码 =》登录页 与 验证码
- 黑马程序员—java基础复习—多线程
- jquery选择器