多线程获取名字Thread类方法getName

子类NameThread继承Thread:

/*
 * 获取线程名字,父类Thread方法
 *         String getName()          非静态的类名调用
 */
public class NameThread extends Thread {
    public void run(){
        System.out.println(super.getName());
    }

}


测试Demo类:

/*
 * 每个线程,都有自己的名字
 * 运行方法main线程,名字就是“main”
 * 其他新建的线程也有名字,默认“Thread-0”,“Thread-1”...
 *
 * JVM开启了主线程,来运行方法main,主线程也是线程,是线程必然就是Thread类对象
 * Thread类中,定义了一个静态方法  static Thread currentThread():返回正在执行的线程对象
 *
 */
public class ThreadDemo {
    public static void main(String[] args) {
        NameThread nt=new NameThread();
        nt.start();
        
        Thread t= Thread.currentThread();
        System.out.println(t.getName());
    }
}


猜你喜欢

转载自blog.csdn.net/summoxj/article/details/80829250