2012-06-02 67 views

回答

6

您可以在您的代碼的任何部分仍然有引用原始線程,並且只要SecurityManager允許它。

8

該函數被稱爲setName(String name);

Thread t = new Thread(...); 
t.start(); 
t.setName("..."); 
0

線程名稱可以使用Thread.currentThread().setName

public class NamedThreadExample implements Runnable { 

    public static final String THREAD_NAME_GOES_HERE = "Original Name"; 
    public static final String NEW_THREAD_NAME_GOES_HERE = "New Name"; 

    public void run() { 
    System.out.println("I am a Thread and my name is " 
         + Thread.currentThread().getName()); 

    Thread.currentThread().setName(NEW_THREAD_NAME_GOES_HERE); 

    System.out.println("I am a Thread and my name is " 
         + Thread.currentThread().getName()); 
    } 

    public static void main(String args[]) { 
    (new Thread(new NamedThreadExample(), THREAD_NAME_GOES_HERE)).start(); 
    } 
} 

事實產生輸出後也實例化Thread對象時設置

I am a Thread and my name is Original Name 
I am a Thread and my name is New Name 

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#Thread%28java.lang.Runnable,%20java.lang.String%29

+0

這個答案如何與更改線程名稱後的線程名稱相關? – Polygnome

相關問題