在Java中,可以通過Thread類的getName()方法來獲取線程的名稱。例如:
Thread t = new Thread();
String threadName = t.getName();
System.out.println("Thread name: " + threadName);
另外,可以使用Thread.currentThread()方法來獲取當前執行線程的引用,然后調用getName()方法來獲取當前線程的名稱,例如:
Thread currentThread = Thread.currentThread();
String currentThreadName = currentThread.getName();
System.out.println("Current thread name: " + currentThreadName);