Java多线程中,Thread.currentThread().getName() VS this.currentThread().getName() VS this.getName()

currentThread() 可以返回 调用 代码段 的线程信息。
先上一个简单实例

public class ThreadNameTest {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName());
    }
}

输出

main

Process finished with exit code 0

再上一个稍微复杂的实例,先定义一个线程类。

class ThreadAA extends Thread {
    public ThreadAA() {
        System.out.println("构造方法中");
        System.out.println("Thread.currentThread().getName()\t" + Thread.currentThread().getName());
        System.out.println("this.currentThread().getName()\t" + this.currentThread().getName());
        System.out.println("this.getName()\t" + this.getName());
    }

    @Override
    public void run() {
        super.run();
        System.out.println("run方法中");
        System.out.println("Thread.currentThread().getName()\t" + Thread.currentThread().getName());
        System.out.println("this.currentThread().getName()\t" + this.currentThread().getName());
        System.out.println("this.getName()\t" + this.getName());
    }
}

此时的main方法为

public class ThreadNameTest {
    public static void main(String[] args) {
        new ThreadAA().start();
        //new ThreadAA().run();
    }
}

此时的输出为

构造方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0
run方法中
Thread.currentThread().getName()	Thread-0
this.currentThread().getName()	Thread-0
this.getName()	Thread-0

Process finished with exit code 0

观察输出结果,不难发现:
ThreadAA 类的构造函数 是被 main线程 调用的。
run方法 自动被 名称为 Thread-0 的线程 所调用。

修改 上述 main方法

public class ThreadNameTest {
    public static void main(String[] args) {
        //new ThreadAA().start();
        new ThreadAA().run();
    }
}

此时的输出为

构造方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0
run方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0

Process finished with exit code 0

修改 上述 main方法

public class ThreadNameTest {
    public static void main(String[] args) {
        //new ThreadAA().start();
        //new ThreadAA().run();
        Thread t1 = new Thread(new ThreadAA());
        t1.setName("t1");
        t1.start();
        //t1.run();
    }
}

此时的输出为

构造方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0
run方法中
Thread.currentThread().getName()	t1
this.currentThread().getName()	t1
this.getName()	Thread-0

Process finished with exit code 0

修改 上述 main方法

public class ThreadNameTest {
    public static void main(String[] args) {
        //new ThreadAA().start();
        //new ThreadAA().run();
        Thread t1 = new Thread(new ThreadAA());
        t1.setName("t1");
        //t1.start();
        t1.run();
    }
}

此时的输出为

构造方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0
run方法中
Thread.currentThread().getName()	main
this.currentThread().getName()	main
this.getName()	Thread-0

Process finished with exit code 0

从以上实例,不难看出
Thread.currentThread().getName() 和 this.currentThread().getName() 是一回事,返回的是 调用者的名称。
this.getName() 返回的是 当前对象的名称。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值