How many threads are allocated by different connection pools in the case of a large number of thread requirements
Executors.newcachedthreadpool (); There will be more than 70,000;
Packagecom.stono.thread2;Importjava.lang.management.ManagementFactory;ImportJava.lang.management.ThreadInfo;ImportJava.lang.management.ThreadMXBean;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.TimeUnit; Public classthreadpoolnewcached { Public Static voidMain (string[] args) {Executorservice Cachedthreadpool=Executors.newcachedthreadpool (); for(inti = 0; I < 100000; i++) {Cachedthreadpool.execute (NewRunnable () {@Override Public voidrun () {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); There will be 10 of them.
Packagecom.stono.thread2;Importjava.lang.management.ManagementFactory;ImportJava.lang.management.ThreadInfo;ImportJava.lang.management.ThreadMXBean;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.ScheduledExecutorService;ImportJava.util.concurrent.TimeUnit; Public classThreadpoolnewschedule { Public Static voidMain (string[] args) {Scheduledexecutorservice Scheduledthreadpool= Executors.newscheduledthreadpool (5); for(inti = 0; I < 100000; i++) {Scheduledthreadpool.execute (NewRunnable () {@Override Public voidrun () {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 (); There will be 6 of them.
Packagecom.stono.thread2;Importjava.lang.management.ManagementFactory;ImportJava.lang.management.ThreadInfo;ImportJava.lang.management.ThreadMXBean;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.TimeUnit; Public classThreadpoolsingle { Public Static voidMain (string[] args) {Executorservice singlethreadexecutor=Executors.newsinglethreadexecutor (); for(inti = 0; I < 100000; i++) {Singlethreadexecutor.execute (NewRunnable () {@Override Public voidrun () {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); There will be 8 of them.
Packagecom.stono.thread2;Importjava.lang.management.ManagementFactory;ImportJava.lang.management.ThreadInfo;ImportJava.lang.management.ThreadMXBean;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.TimeUnit; Public classthreadpoolfixed { Public Static voidMain (string[] args) {Executorservice Fixedthreadpool= Executors.newfixedthreadpool (3); for(inti = 0; I < 100000; i++) {Fixedthreadpool.execute (NewRunnable () {@Override Public voidrun () {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 ()); } } }
How many threads are allocated by different connection pools in the case of a large number of thread requirements