Java Thread setName()方法

java.lang.Thread.setName() 方法改变参数名等于该线程的名称。

1 语法

public final void setName(String name)

2 参数

name:这是用于该线程的新名称

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.setName()方法的例子
 */
import java.lang.*;

public class Demo {

    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 = 2

热门文章

优秀文章