共計(jì) 596 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Java 中,你可以使用 Thread.currentThread()
方法來(lái)獲取當(dāng)前正在執(zhí)行的線程對(duì)象。這個(gè)方法返回一個(gè)代表當(dāng)前線程的 Thread
對(duì)象,你可以通過(guò)該對(duì)象來(lái)獲取線程的相關(guān)信息。下面是一個(gè)簡(jiǎn)單的示例代碼:
public class Main {public static void main(String[] args) {Thread currentThread = Thread.currentThread();
System.out.println("Current thread: " + currentThread.getName());
System.out.println("Thread ID: " + currentThread.getId());
System.out.println("Thread state: " + currentThread.getState());
// 可以根據(jù)需要查看更多線程的信息
}
}
Thread.currentThread()
返回當(dāng)前正在執(zhí)行的線程。currentThread.getName()
返回當(dāng)前線程的名稱。currentThread.getId()
返回當(dāng)前線程的 ID。currentThread.getState()
返回當(dāng)前線程的狀態(tài)。
當(dāng)你運(yùn)行這段代碼時(shí),它會(huì)輸出當(dāng)前線程的名稱、ID 和狀態(tài)等信息。你可以根據(jù)需要查看更多關(guān)于當(dāng)前線程的信息。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完