public class Test extends Thread { public void run() { printMsg();// 从多线程调用,因此会打印该线程名称 } public void printMsg() { // 获得运行此代码的线程的引用 Thread t = Thread.currentThread(); String name = t.getName(); System.out.println("name = " + name); } public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); Test t3 = new Test(); t1.start(); t2.start(); t3.start(); t1.printMsg(); // 从主线程调用,因此会打印main } }