Java多線程之this與Thread.currentThread()的區別——java多線程編程核心技術



package
mythread; public 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("this.getName=" + this.getName()); System.out.println("this.isAlive()=" + this.isAlive()); System.out.println("CountOperate---end "); System.out.println("Thread.currentThread()==this :"+ (Thread.currentThread() == this)); } @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"); } }
 
public class Run {

    public static void main(String[] args){

        CountOperate c = new CountOperate();
        c.start();
        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());

    }
}

打印的log為:

CountOperate---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
CountOperate---end
Thread.currentThread()==this :false
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()=Thread-0
this.isAlive()=false
run --- end

根據打印的Log可以知道調用CountOperate構造函數的是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源碼發現,在Thread類的構造方法中,會自動給name賦值,賦值代碼:

然后執行到:
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()創建的那個線程實例,而不是new Thread(thread)創建的那個實例即t1。
查看源代碼可以知道。

實際上new Thread(thread)會將thread應用的對象綁定到一個pravite變量target上,
在t1被執行的時候即t1.run()被調用的時候,它會調用target.run()方法,也就是說它是直接調用thread對象的run方法,
再確切的說,在run方法被執行的時候,this.getName()實際上返回的是target.getName(),而Thread.currentThread().getName()實際上是t1.getName()。

因此我們修改下main中的代碼為:
public class Run {

    public static void main(String[] args){

        CountOperate c = new CountOperate();
        c.start();
    }
}

打印的log為:

CountOperate---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
CountOperate---end 
Thread.currentThread()==this :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

與我們預想的結果相同






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM