當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Thread currentThread()用法及代碼示例

線程類的 currentThread() 方法用於返回對當前正在執行的線程對象的引用。

用法

public static Thread currentThread()

返回值

它返回當前正在執行的線程。

示例

public class CurrentThreadExp extends Thread
{  
    public void run()
    {  
        // print currently executing thread 
        System.out.println(Thread.currentThread().getName());  
    }  
    public static void main(String args[])
    {  
        // creating two thread
        CurrentThreadExp t1=new CurrentThreadExp();  
        CurrentThreadExp t2=new CurrentThreadExp();  
        // this will call the run() method
        t1.start();  
        t2.start();  
    }  
}

輸出:

Thread-0
Thread-1






相關用法


注:本文由純淨天空篩選整理自 Java Thread currentThread() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。