/** * Created by xabcd on 2019/2/17. */ class GetNameThread extends Thread { public void run() { for (int i = 0; i < 10; i++) printMsg(); // } public void printMsg() { Thread ge = Thread.currentThread(); String name = ge.getName(); System.out.println("name = "+name); } public static void main(String[] args) { GetNameThread ge = new GetNameThread(); ge.start(); for(int i = 0;i<10;i++) { ge.printMsg(); } } } 结果: name = main name = main name = main name = main name = main name = main name = main name = main name = main name = main name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0 name = Thread-0