
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.CountDownLatch;


public class GetPeakThreadCount {

    public static void main(String[] args) throws InterruptedException {
        new GetPeakThreadCount().test01();
    }


    public void test01() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(4);
        Thread v1 = Thread.ofVirtual().unstarted(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread v2 = Thread.ofVirtual().unstarted(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread p1 = Thread.ofPlatform().unstarted(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        //daemon
        Thread p2 = Thread.ofPlatform().daemon().unstarted(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        int originalCount = threadMXBean.getPeakThreadCount();
        v1.start();
        v2.start();
        p1.start();
        p2.start();
        Thread.sleep(5);
        int latestCount = threadMXBean.getPeakThreadCount();
        System.out.println("The values are " + originalCount + " " + latestCount);
        for (int i = 0; i < latch.getCount(); i++) {
            latch.countDown();
        }
    }


}
