线程类的 setName() 方法用于更改线程的名称。
用法
public final void setName(String a)
参数
a = It shows the new name for the thread.
返回
This method does not return any value.
异常
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("Sonoo Jaiswal");
t2.setName("javatpoint");
// 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:Sonoo Jaiswal running... running... After changing name of t2:javatpoint
相关用法
- Java Thread setDefaultUncaughtExceptionHandler()用法及代码示例
- Java Thread setContextClassLoader()用法及代码示例
- Java Thread setDaemon()用法及代码示例
- Java Thread setPriority()用法及代码示例
- Java Thread suspend()用法及代码示例
- Java Thread sleep()用法及代码示例
- Java Thread stop()用法及代码示例
- Java Thread start()用法及代码示例
- Java Thread toString()用法及代码示例
- Java Thread interrupted()用法及代码示例
- Java Thread destroy()用法及代码示例
- Java Thread holdLock()用法及代码示例
- Java Thread getContextClassLoader()用法及代码示例
- Java Thread getThreadGroup()用法及代码示例
- Java Thread isInterrupted()用法及代码示例
- Java Thread enumerate()用法及代码示例
- Java Thread notify()用法及代码示例
- Java Thread resume()用法及代码示例
- Java Thread activeCount()用法及代码示例
- Java Thread isDaemon()用法及代码示例
注:本文由纯净天空筛选整理自 Java Thread setName() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。