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

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

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

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

    
}

 

上一篇:陈春花推荐的三本书是什么 back to basic -- management


下一篇:镜像服务(Image Management Service,IMS)