Java Thread getId()方法

getId()方法用於返回線程識別字。線程ID是在創建線程時生成的唯一正數值。

線程ID在其生命週期內保持不變。線程終止時,可以重用線程的ID。

語法

public long getId()

返回值

此方法返回線程的ID。

示例

public class GetIdExample extends Thread
{
    public void run()
    {
        System.out.println("running...");
    }
    public static void main(String args[])
    {
        // creating one thread
        GetIdExample t1=new GetIdExample();
        // Returns the identifier of this Thread
        System.out.println("Name of t1: "+t1.getName());
        System.out.println("Id of t1: "+t1.getId());
        // Start the thread
        t1.start();
    }
}

執行上面示例代碼,得到以下結果:

Name of t1: Thread-0
Id of t1: 39
running...

上一篇: Java Runtime類 下一篇:無