设置线程名称以及获取线程名称

package cn.dali3.code01;

import static java.lang.Thread.currentThread;

/*设置线程名称以及获取线程名称
* 获取线程名称的方法:
*   String getNmae();
*
*
* 获取当前线程的方法:
*   static Thread currentThread() 返回当前正在执行线程对象的引用
*   这是一个静态方法  可以直接用currentThread调用
*
*
* 设置线程名称的方法:
*   void setName();
*
*   注意:线程与线程名称并不一样
*   */
public class Demo02 {
    public static void main(String[] args) {
        System.out.println(currentThread().getName());
        MyThread th = new MyThread();
        System.out.println(th.getName());//获取线程名称
        th.start();
        th.setName("A");
        System.out.println(th.getName());//打印改名后的名称

    }


}

你可能感兴趣的:(JAVA)