总结起来一句话:在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)的源码
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
public class TestThread { public static void main(String[] args) {
Bed b = new Bed();
5 b.start();
6 Thread t1 = new Thread(b,"B");
System.out.println("main begin t1 isAlive=" + t1.isAlive());
// t1.setName("B");
t1.start();
System.out.println("main end t1 isAlive=" + t1.isAlive()); }
} class Bed extends Thread { public Bed() {
System.out.println("Bed Constructor---begin");
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("Bed Constructor---end ");
} @Override
public void run() {
System.out.println("run---begin");
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()" + Thread.currentThread().isAlive());
System.out.println("Thread.currentThread()==this :" + (Thread.currentThread() == this));
System.out.println("this.getName()=" + this.getName());
System.out.println("this.isAlive()=" + this.isAlive());
System.out.println("run --- end");
}
}
结果:
Bed Constructor---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
Thread.currentThread()==this :false
Bed Constructor---end
main begin t1 isAlive=false
run---begin
Thread.currentThread().getName=Thread-0
Thread.currentThread().isAlive()true
Thread.currentThread()==this :true
this.getName()=Thread-0
this.isAlive()=true
run --- end
main end t1 isAlive=true
run---begin
Thread.currentThread().getName=B
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false
this.getName()=Thread-0
this.isAlive()=false
run --- end
b.start()的时候,就是调用Thread的run方法,而newThread对象的时候将其赋给target对象,如果未指定名称系统默认给target命名,target是线程类Thread的一个私有Runnable类型属性。
private Runnable target;
即使将target和new Thread的线程赋值一样的名称也不会出现this==Thread.currentThread(),从而说明target和new Thread()是两个不同的对象。
public class TestThread { public static void main(String[] args) { Bed b = new Bed("A");
b.start();
Thread t1 = new Thread(b,"A");
System.out.println("main begin t1 isAlive=" + t1.isAlive());
t1.start();
System.out.println("main end t1 isAlive=" + t1.isAlive()); } }
结果:
Bed Constructor---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=A
this.isAlive()=false
Thread.currentThread()==this :false
Bed Constructor---end
run---begin
Thread.currentThread().getName=A
Thread.currentThread().isAlive()true
Thread.currentThread()==this :true
this.getName()=A
this.isAlive()=true
run --- end
main begin t1 isAlive=false
main end t1 isAlive=true
run---begin
Thread.currentThread().getName=A
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false
this.getName()=A
this.isAlive()=false
run --- end
Reference
[1] https://www.cnblogs.com/huangyichun/p/6071625.html