当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Thread setName()用法及代码示例


线程类的 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 setName() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。