码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-05-09 14:58:49      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:runnable   import   ade   []   read   连接池   sleep   分配   main   

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

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

    
}

 

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

标签:runnable   import   ade   []   read   连接池   sleep   分配   main   

原文地址:https://www.cnblogs.com/stono/p/9013558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!