zoukankan      html  css  js  c++  java
  • 不同连接池在大量线程需求的情况下都分配了多少线程

    不同连接池在大量线程需求的情况下都分配了多少线程

    Executors.newCachedThreadPool(); 会有7万多个;

    package com.stono.thread2;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.lang.management.ThreadMXBean;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolNewCached  {
    
        public static void main(String[] args) {
            ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
            for (int i = 0; i < 100000; i++) {
                cachedThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }    
                    }
                });
            }
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false);
            System.out.println(dumpAllThreads.length);
            for (ThreadInfo threadInfo : dumpAllThreads) {
    //            System.out.println(threadInfo.getThreadName());
            }
        }
    
        
    }

    Executors.newScheduledThreadPool(5); 一共会有10个

    package com.stono.thread2;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.lang.management.ThreadMXBean;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolNewSchedule  {
    
        public static void main(String[] args) {
            ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
            for (int i = 0; i < 100000; i++) {
                scheduledThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }    
                    }
                });
            }
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false);
            System.out.println(dumpAllThreads.length);
            for (ThreadInfo threadInfo : dumpAllThreads) {
                System.out.println(threadInfo.getThreadName());
            }
        }
    
        
    }

    Executors.newSingleThreadExecutor(); 一共会有6个

    package com.stono.thread2;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.lang.management.ThreadMXBean;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolSingle  {
    
        public static void main(String[] args) {
            ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
            for (int i = 0; i < 100000; i++) {
                singleThreadExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }    
                    }
                });
            }
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false);
            System.out.println(dumpAllThreads.length);
            for (ThreadInfo threadInfo : dumpAllThreads) {
                System.out.println(threadInfo.getThreadName());
            }
        }
    
        
    }

    Executors.newFixedThreadPool(3); 一共会有8个

    package com.stono.thread2;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.lang.management.ThreadMXBean;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolFixed  {
    
        public static void main(String[] args) {
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
            for (int i = 0; i < 100000; i++) {
                fixedThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }    
                    }
                });
            }
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false);
            System.out.println(dumpAllThreads.length);
            for (ThreadInfo threadInfo : dumpAllThreads) {
                System.out.println(threadInfo.getThreadName());
            }
        }
    
        
    }
  • 相关阅读:
    Dyanmcis 365调用Action报Entity Reference cannot have Id and Key Attributes empty.错误
    Dynamics 365中使用工作流发邮件让其可以发往文本字段指定的邮箱
    Dynamics 365 Web API分页查询数据
    微软Dynamics CRM 2013介绍系列之三十:筛选查找控件,so easy。
    Power Automate实用常见问题解答(FAQ)
    Dynamics 365使用JavaScript调用Web API批量设置字段的审核属性为禁用。
    Dynamics 365 V9版本新的客户端API Xrm.WebApi.online.execute 使用实例
    Dynamics 365附件的常见控制
    Dynamics 365的存储容量介绍
    请不要在繁忙时候更改用户的业务部门
  • 原文地址:https://www.cnblogs.com/stono/p/9013558.html
Copyright © 2011-2022 走看看