Thread.currentThread()和this

第一個例子

看一個很簡單的例子:

public class MyThread extends Thread {

    public MyThread() {
        super();
        System.out.println("構造方法currentThread:"+Thread.currentThread().getName());
        System.out.println("構造方法this:"+this.getName());
    }

    @Override
    public void run() {
        System.out.println("run方法currentThread:"+Thread.currentThread().getName());
        System.out.println("run方法this:"+this.getName());
    }
}
public class TestMain {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.setName("myThread");
        myThread.start();
    }
}

輸出如下

構造方法currentThread:main
構造方法this:Thread-0
run方法currentThread:myThread
run方法this:myThread

第一行:構造方法中Thread.currentThread().getName()很明顯當前執(zhí)行的線程是main線程
第二行:構造方法中的this.getName()為什么是Thread-0呢?點進Thread類的無參構造方法中看到源碼如下,Thread為每一個線程默認取了一個名字

    public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);
    }

第三行:run方法中Thread.currentThread().getName()),當前執(zhí)行的線程就是myThread,執(zhí)行run方法之前我們已經(jīng)為myThread對象設置了名字,所以name為myThread,誰調(diào)用start()方法,currentThread就是誰。
第四行:run方法中的this.getName(),而run方法中this對象就是myThread對象,在這個例子中,我們首先調(diào)用了myThread對象的start方法,start方法又回調(diào)了myThread類的run方法,所以自始至終都只有myThread對象一個


image.png

第二個例子

MyThread類不變,測試類修改為如下,把myThread對象當做Thread的參數(shù)傳進去

public class TestMain {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.setName("myThread");
        Thread thread=new Thread(myThread,"thread");
        thread.start();
    }
}

輸出如下:

構造方法currentThread:main
構造方法this:Thread-0
run方法currentThread:thread
run方法this:myThread

前兩個就不用說了,第三行run方法中currentThread為thread對象,誰調(diào)用的start()方法,currentThread就是誰。
第四行run方法中的this為myThread對象,而在這個例子中,我們首先調(diào)用了thread對象的start()方法,thread對象的start方法回調(diào)了thread類的run方法,thread類的run方法中調(diào)用了target的run方法,所以this就指向了target,而target正是我們傳入的myThread對象


image.png

image.png

看一下Thread的有參構造方法

    public Thread(Runnable target, String name) {
        init(null, target, name, 0);
    }

再看Thread類的run方法

    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

關于為什么調(diào)用了start()方法會執(zhí)行run()方法參考此篇文章https://blog.csdn.net/qq_38799155/article/details/78488161

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 在正式學習Thread類中的具體方法之前,我們先來了解一下線程有哪些狀態(tài),這個將會有助于后面對Thread類中的方...
    朦朧蜜桃閱讀 911評論 1 0
  • Java多線程學習 [-] 一擴展javalangThread類 二實現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,108評論 1 18
  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,597評論 1 15
  • 摘要: 我們已經(jīng)知道,synchronized 是Java的關鍵字,是Java的內(nèi)置特性,在JVM層面實現(xiàn)了對臨界...
    kingZXY2009閱讀 1,881評論 0 20
  • 一個月沒見你動態(tài),或許是不夠自由。往后你自由,想說的話可以告訴別人,會有人陪你度過將來無數(shù)的長夜漫漫。別把回憶當負...
    謝小亦_閱讀 212評論 0 0

友情鏈接更多精彩內(nèi)容