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

Posted stono

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不同连接池在大量线程需求的情况下都分配了多少线程相关的知识,希望对你有一定的参考价值。

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

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

    
}

 

以上是关于不同连接池在大量线程需求的情况下都分配了多少线程的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程——线程池

javaSE-多线程

理解JVM之内存分配以及分代思想实现

线程池在爬虫案例中的应用

Java 中的线程池

Java线程池在运行后的结果反查