查看java中的線程個數名稱
阿新 • • 發佈:2018-05-09
string ack println art @override static out java rgs
查看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(inti) { 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()); } } @Overridepublic void run() { System.out.println(i); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }
查看java中的線程個數名稱