java 线程 this_Java 多线程 ---- 线程中this与 Thread.currentThread()线程的区别

本文详细探讨了Java中线程的this引用与Thread.currentThread()的区别。通过实例代码分析,指出在创建Thread对象时,this实际上是绑定到Thread的私有属性target上,而Thread.currentThread()返回的是当前正在执行的线程对象,两者并不相同。
摘要由CSDN通过智能技术生成

总结起来一句话:在Thread中调用this其实就是调用Thread私有Runnable类型的target,target是Thread类的一个属性,而Thread.currentThread()是指新New出来的实例Thread类。两个是不同的对象。实例化一个Thread的对象,都会将其赋值给Thread的私有target属性。

直接上代码:

注意代码中红色部分,就可以解释this和Thread.currentThread()的区别。实际上new Thread(Runnable),new Thread(Runnable,String) 会将一个thread( Thread 是Runnable接口的实现类) 应用的对象绑定到一个pravite变量target上,在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法,再确切的说,在run方法被执行的时候,this.getName()实际上返回的target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。

new Thread(Runnable)的源码

1 publicThread(Runnable target) {2 init(null, target, "Thread-" + nextThreadNum(), 0);3 }

1 public classTestThread {2

3 public static voidmain(String[] args) {4 Bed b = new Bed();

5 b.start();

6 Thread t1 = new Thread(b,"B");7 System.out.println("main begin t1 isAlive=" +t1.isAlive());8 //t1.setName("B");

9 t1.start();10 System.out.println("main end t1 isAlive=" +t1.isAlive());11

12 }13 }14

15 class Bed extendsThread {16

17 publicBed() {18 System.out.println("Bed Constructor---begin");19 System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());//获取线程名

20 System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive()); //查看线程是否存活

21 System.out.println("this.getName=" + this.getName());22 System.out.println("this.isAlive()=" + this.isAlive());23 System.out.println("Thread.currentThread()==this :" + (Thread.currentThread() == this));24 System.out.println("Bed Constructor---end ");25 }26

27 @Override28 public voidrun() {29 System.out.println("run---begin");30 System.out.println("Thread.currentThread().getName=" +Thread.currentThread().getName());31 System.out.println("Thread.currentThread().isAlive()" +Thread.currentThread().isAlive());32 System.out.println("Thread.currentThread()==this :" + (Thread.currentThread() == this));33 System.out.println("this.getName()=" + this.getName());34 System.out.println("this.isAlive()=" + this.isAlive());35 System.out.println("run --- end");36 }37 }

结果:

1 Bed Constructor---begin2 Thread.currentThread().getName()=main3 Thread.currentThread().isAlive()=true

4 this.getName=Thread-0

5 this.isAlive()=false

6 Thread.currentThread()==this :false

7 Bed Constructor---end8 main begin t1 isAlive=false

9 run---begin10 Thread.currentThread().getName=Thread-0

11 Thread.currentThread().isAlive()true

12 Thread.currentThread()==this :true

13 this.getName()=Thread-0

14 this.isAlive()=true

15 run ---end16 main end t1 isAlive=true

17 run---begin18Thread.currentThread().getName=B19 Thread.currentThread().isAlive()true

20 Thread.currentThread()==this :false

21 this.getName()=Thread-0

22 this.isAlive()=false

23 run --- end

b.start()的时候,就是调用Thread的run方法,而newThread对象的时候将其赋给target对象,如果未指定名称系统默认给target命名,target是线程类Thread的一个私有Runnable类型属性。

1 private Runnable target;

即使将target和new Thread的线程赋值一样的名称也不会出现this==Thread.currentThread(),从而说明target和new Thread()是两个不同的对象。

1 public classTestThread {2

3 public static voidmain(String[] args) {4

5 Bed b = new Bed("A");6 b.start();7 Thread t1 = new Thread(b,"A");8 System.out.println("main begin t1 isAlive=" +t1.isAlive());9 t1.start();10 System.out.println("main end t1 isAlive=" +t1.isAlive());11

12 }13

14 }

结果:

1 Bed Constructor---begin2 Thread.currentThread().getName()=main3 Thread.currentThread().isAlive()=true

4 this.getName=A5 this.isAlive()=false

6 Thread.currentThread()==this :false

7 Bed Constructor---end8 run---begin9 Thread.currentThread().getName=A10 Thread.currentThread().isAlive()true

11 Thread.currentThread()==this :true

12 this.getName()=A13 this.isAlive()=true

14 run ---end15 main begin t1 isAlive=false

16 main end t1 isAlive=true

17 run---begin18 Thread.currentThread().getName=A19 Thread.currentThread().isAlive()true

20 Thread.currentThread()==this :false

21 this.getName()=A22 this.isAlive()=false

23 run --- end

Reference

[1] https://www.cnblogs.com/huangyichun/p/6071625.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值