天天看點

在不是Thread類的子類中,如何擷取線程對象的名稱呢?

我想要擷取main方法所在的線程對象的名稱,該怎麼辦呢?

  遇到這種情況,Thread類就提供了一個很好玩的方法:

    public static Thread currentThread() 傳回目前正在執行的線程對象

package cn.itcast_03;

/*
 * 在不是Thread類的子類中,如何擷取線程對象的名稱呢?
 *         public static Thread currentThread() 傳回目前正在執行的線程對象(靜态方法)
 *             Thread.currentThread().getName()
 */
public class MyThreadDemo {
    public static void main(String[] args) {
        // 我要擷取main方法所在的線程對象的名稱,該怎麼辦呢?
        // 遇到這種情況,Thread類就提供了一個很好玩的靜态方法:
        // public static Thread currentThread() 傳回目前正在執行的線程對象
        System.out.println(Thread.currentThread().getName()); // main
    }
}      

我的GitHub位址:

https://github.com/heizemingjun

我的部落格園位址:

http://www.cnblogs.com/chenmingjun

我的螞蟻筆記部落格位址:

http://blog.leanote.com/chenmingjun

Copyright ©2018 黑澤明軍

【轉載文章務必保留出處和署名,謝謝!】