String getName()
描述 (Description)
java.lang.Thread.getName()方法返回此线程的名称。
声明 (Declaration)
以下是java.lang.Thread.getName()方法的声明
public final String getName()
参数 (Parameters)
NA
返回值 (Return Value)
此方法返回此线程的名称。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Thread.getName()方法的用法。
package com.iowiki;
import java.lang.*;
public class ThreadDemo implements Runnable {
Thread t;
ThreadDemo() {
// thread created
t = new Thread(this, "Admin Thread");
// set thread priority
t.setPriority(1);
// print thread created
System.out.println("thread = " + t);
// this will call run() function
t.start();
}
public void run() {
// returns the name of this Thread.
System.out.println("Name = " + t.getName());
}
public static void main(String args[]) {
new ThreadDemo();
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
thread = Thread[Admin Thread,1,main]
Name = Admin Thread