目录

void setName(String name)

描述 (Description)

java.lang.Thread.setName()方法将此线程的名称更改为等于参数name

声明 (Declaration)

以下是java.lang.Thread.setName()方法的声明

public final void setName(String name)

参数 (Parameters)

name - 这是此线程的新名称

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

SecurityException - 如果当前线程无法修改此线程。

例子 (Example)

以下示例显示了java.lang.Thread.setName()方法的用法。

package com.iowiki;
import java.lang.*;
public class ThreadDemo {
   public static void main(String[] args) {
      Thread t = Thread.currentThread();
      // prints the thread name
      System.out.println("Thread = " + t);
      // change the thread name
      t.setName("Admin Thread");
      // prints the thread after changing name
      System.out.println("Thread after changing name = " + t);
      int count = Thread.activeCount();
      System.out.println("currently active threads = " + count);
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

Thread = Thread[main,5,main]
Thread after changing name = Thread[Admin Thread,5,main]
currently active threads = 1
↑回到顶部↑
WIKI教程 @2018