查看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();
        }
    }
}

 

你可能感兴趣的:(查看java中的线程个数名称)