在自定義線程類時,若是線程類是繼承java.lang.Thread的話,那麼線程類就可使用this關鍵字去調用繼承自父類Thread的方法,this就是當前的對象。java
另外一方面,Thread.currentThread()能夠獲取當前線程的引用,通常都是在沒有線程對象又須要得到線程信息時經過Thread.currentThread()獲取當前代碼段所在線程的引用。ide
儘管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(); } }
測試結果: this
------構造函數開始------
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()結束------線程
解釋:code
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true對象
實例化MyThread,調用MyThread構造方法是主線程main繼承
this.getName() = Thread-0
this.isAlive() = falseget
如今,這個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的引用,因此是不同的,打印的內容也不同.