Thread
類的setName()
方法用於更改執行緒的名稱。
語法
public final void setName(String a)
引數
a
- 執行緒要設定的新名稱。異常
SecurityException
:如果當前執行緒無法修改執行緒,則丟擲此異常。範例程式碼
public class SetNameExample extends Thread
{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating two threads
SetNameExample t1=new SetNameExample();
SetNameExample t2=new SetNameExample();
// start of thread
t1.start();
t2.start();
// change the thread name
t1.setName("Max Su");
t2.setName("yiibai");
// print the thread after changing
System.out.println("After changing name of t1: "+t1.getName());
System.out.println("After changing name of t2: "+t2.getName());
}
}
執行上面範例程式碼,得到以下結果:
After changing name of t1: Max Su
running...
running...
After changing name of t2: yiibai