多线程中this、Thread.currentThread()和this.currentThread()的区别

 

写了如下代码进行试验

public class CountOperate extends Thread {
    public void run()
    {
        try {
            System.out.println("run begin");
            System.out.println("run threadName=" + this.currentThread().getName());
            System.out.println("thread cur thread"+Thread.currentThread().getName());
            System.out.println("this thread name"+this.getName());
            Thread.sleep(2000);

          /* System.out.println("run threadName="+this.currentThread().getName()+"end");*/
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
public class Run
{
    public static void main(String args[])
    {
        CountOperate count = new CountOperate();
        Thread thread = new Thread(count);
        thread.start();
        /*thread.run();*/

    }

}

打印的结果如下所示

 

run begin
run threadName=Thread-1
thread cur threadThread-1
this thread nameThread-0

Process finished with exit code 0

Thread.currentThread()和this.currentThread()在大部分情况下结果是相同的,都是调用Thread的静态方法currentThread(),若当前子类重写了父类的currentThread()方法,那么Thread.currentThread()和this.currentThread()返回的结果会不同。

若执行的代码如下所示

public class CountOperate extends Thread {




    public void run()
    {
        try {
            System.out.println("run begin");
            System.out.println("run threadName=" + this.currentThread().getName());
            System.out.println("thread cur thread"+Thread.currentThread().getName());
            System.out.println("this thread name"+this.getName());
            Thread.sleep(2000);

          /* System.out.println("run threadName="+this.currentThread().getName()+"end");*/
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
public class Run
{
    public static void main(String args[])
    {
        CountOperate count = new CountOperate();
        Thread thread = new Thread(count);
       /* thread.start();*/
        thread.run();
    }
}

程序的输出如下所示

run begin
run threadName=main
thread cur threadmain
this thread nameThread-0

Process finished with exit code 0
 

先来说说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()
  • 1

有兴趣的同学可以自己去查源码! 
这就解释了为什么“this.getName() = Thread-0”。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值