public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate();
Thread t1 = new Thread(c);
System.out.println("main begin t1 isAlive = " + t1.isAlive());
t1.setName("A");
t1.start();
System.out.println("main end t1 isAlive = " + t1.isAlive());
System.out.println("main end c isAlive = " + c.isAlive());
}
}
class CountOperate extends Thread {
public CountOperate(){
System.out.println("CountOperate --- 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("CountOperate --- 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");
}
}
結(jié)果:
CountOperate --- begin
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
Thread.currentThread() == this : false
this.getName = Thread-0
this.isAlive() = false
CountOperate --- end
main begin t1 isAlive = false
main end t1 isAlive = true
main end c isAlive = false
run --- begin
Thread.currentThread().getName = A
Thread.currentThread().isAlive() = true
Thread.currentThread() == this : false
this.getName() = Thread-0
this.isAlive() = false
run --- end
根據(jù)打印的 Log 可以知道調(diào)用 CountOperate 構(gòu)造函數(shù)的是 main 線程,因此打印出:
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
而此時還沒有啟動 CountOperate 子線程所以打印出:
this.getName = Thread-0
this.isAlive() = false
此時 this 代表的是 CountOperate 對象實例盛嘿,所以
Thread.currentThread() == this : false
這里比較讓人疑惑的是this.getName() = Thread-0
,這個 Thread-0 是什么東西
通過查看 Thread 源碼發(fā)現(xiàn)须肆,在 Thread 類的構(gòu)造方法中,會自動給 name 賦值:
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
然后執(zhí)行到:
Thread t1 = new Thread(c);
System.out.println("main begin t1 isAlive = " + t1.isAlive());
t1.setName("A");
t1.start();
Log 打佣タ肌:
Thread.currentThread().getName = A
Thread.currentThread().isAlive() = true
Thread.currentThread() == this : false
this.getName() = Thread-0
this.isAlive() = false
說明此時的 this 和 Thread.currentThread() 指向不是同一個線程實例
也就是說酱床,this 指向的還是 new CountOperate() 創(chuàng)建的那個線程實例 c,而不是 new Thread(thread) 創(chuàng)建的那個實例即 t1独悴。
查看源代碼可以知道:
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
實際上 new Thread(thread) 會將 thread 應(yīng)用的對象綁定到一個 private 變量 target 上
在 t1 被執(zhí)行的時候即 t1.run() 被調(diào)用的時候,它會調(diào)用 target.run() 方法
再確切的說锣尉,在 run 方法被執(zhí)行的時候刻炒,this.getName() 實際上返回的是 target.getName(),而 Thread.currentThread().getName() 實際上是 t1.getName()
因為調(diào)用 start() 的線程是 t1自沧,而不是 c坟奥,所以 this.isAlive() = false
如果修改 main 中的代碼為:
public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate();
c.start();
}
}
結(jié)果:
CountOperate --- begin
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
Thread.currentThread() == this : false
this.getName = Thread-0
this.isAlive() = false
CountOperate --- end
run --- begin
Thread.currentThread().getName = Thread-0
Thread.currentThread().isAlive() = true
Thread.currentThread() == this : true
this.getName() = Thread-0
this.isAlive() = true
run --- end
符合預(yù)期