以下實例示範如何使用getName() 方法取得所有正在執行的執行緒:
/* author by w3cschool.cc Main.java */public class Main extends Thread { public static void main(String[] args) { Main t1 = new Main(); t1.setName("thread1"); t1.start(); ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); int noThreads = currentGroup.activeCount(); Thread[] lstThreads = new Thread[noThreads]; currentGroup.enumerate(lstThreads); for (int i = 0; i < noThreads; i++) System.out.println("线程号:" + i + " = " + lstThreads[i].getName()); }}
以上程式碼運行輸出結果為:
线程号:0 = main线程号:1 = thread1
以上就是Java 實例- 取得所有執行緒的內容,更多相關內容請關注PHP中文網(www.php.cn)!