java.lang.Thread.setName()%uA0方法改变参数名等于该线程的名称。
声明
以下是java.lang.Thread.setName()方法的声明
public final void setName(String name)
参数
-
name%uA0-- 这是用于该线程的新名称
返回值
此方法不返回任何值。
异常
-
SecurityException%uA0-- 如果当前线程不能修改该线程。
例子
下面的例子显示java.lang.Thread.setName()方法的使用。
package com.yiibai 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