Thread.currentThread()与this的区别

本文详细解析了在Java中自定义线程类时,this与Thread.currentThread()的区别及应用场景。通过具体代码示例,阐述了两者在不同情况下的引用差异,尤其在线程构造与启动过程中的表现。

    在自定义线程类时,如果线程类是继承java.lang.Thread的话,那么线程类就可以使用this关键字去调用继承自父类Thread的方法,this就是当前的对象。

    另一方面,Thread.currentThread()可以获取当前线程的引用,一般都是在没有线程对象又需要获得线程信息时通过Thread.currentThread()获取当前代码段所在线程的引用。

    尽管this与Thread.currentThread() 都可以获取到Thread的引用,但是在某种情况下获取到的引用是有差别的,下面进行举例说明

package com.thread;

public class MyThread extends Thread {
    public MyThread() {
        System.out.println("------" + "构造函数开始" + "------");
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
        System.out.println("this.getName() = " + this.getName());
        System.out.println("this.isAlive() = " + this.isAlive());
        System.out.println("------" + "构造函数结束" + "------");
    }

    @Override
    public void run() {
        System.out.println("------" + "run()开始" + "------");
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
        System.out.println("this.getName() = " + this.getName());
        System.out.println("this.isAlive() = " + this.isAlive());
        System.out.println("Thread.currentThread() == this : " + (Thread.currentThread() == this));
        System.out.println("------" + "run()结束" + "------");
    }
}

测试类:

package com.thread;

public class Test {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.setName("A");
        myThread.start();
    }
}

测试结果: 

------构造函数开始------
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
------构造函数结束------
------run()开始------
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = A
this.isAlive() = true
Thread.currentThread() == this : true
------run()结束------

解释:

    Thread.currentThread().getName() = main
    Thread.currentThread().isAlive() = true

    实例化MyThread,调用MyThread构造方法是主线程main

    

    this.getName() = Thread-0
    this.isAlive() = false

    现在,这个this是MyThread的引用,是个线程类,但是这个线程类并没有设置名字,所以Thread默认给了一个Thread-0

    因为仅仅是运行构造方法,还未运行线程,所以this.isAlive() = false

    之后是run()中的代码结果:
        Thread.currentThread().getName() = A
        Thread.currentThread().isAlive() = true

    当前线程名字为A,A是我们手动赋予的myThread.setName("A");,并且它是运行着的

        this.getName() = A

        this.isAlive() = true

    因为运行的线程就是MyThread的引用,而this也是MyThread的引用,所以打印结果与Thread.currentThread()相同,并且Thread.currentThread() == this : true

 

    保持线程类不变,如下修改测试类:

package com.thread;

public class Test {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        // 将线程对象以构造参数的方式传递给Thread对象进行start()启动线程
        Thread newThread = new Thread(myThread);
        newThread.setName("A");
        newThread.start();
    }
}

测试结果如下: 

------构造函数开始------
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
------构造函数结束------
------run()开始------
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
Thread.currentThread() == this : false
------run()结束------

    此时,this 与 Thread.currentThread()不是同一个引用

    将线程对象以构造参数的方式传递给Thread对象进行start()启动线程,我们直接启动的线程实际是new Thread,而作为构造参数的myThread,赋给Thread类中的属性target,之后在Thread的run方法中调用target.run(),即this依旧是MyThread的引用;

    此时Thread.currentThread()是Thread的引用new Thread, 而this依旧是MyThread的引用,所以是不一样的,打印的内容也不一样.

转载于:https://my.oschina.net/langwanghuangshifu/blog/2874491

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值