查看java中的線程個數名稱java
package com.stono.thread2; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.concurrent.TimeUnit; public class Runnable2Threads implements Runnable { private int i; public Runnable2Threads(int i) { this.i = i; } public static void main(String[] args) { Runnable2Threads runnable2Threads = new Runnable2Threads(100); for(int i=0;i<10;i++){ Thread thread = new Thread(runnable2Threads); thread.start(); } ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false); for (ThreadInfo threadInfo : dumpAllThreads) { // 雖然runnable是一個,可是線程倒是十個; System.out.println(threadInfo.getThreadName()); } } @Override public void run() { System.out.println(i); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }