// org.apache.tomcat.util.net.AbstractEndpoint#getCurrentThreadsBusy /** * Return the amount of threads that are in use * * @return the amount of threads that are in use */ publicintgetCurrentThreadsBusy(){ if (executor!=null) { if (executor instanceof ThreadPoolExecutor) { return ((ThreadPoolExecutor)executor).getActiveCount(); } elseif (executor instanceof ResizableExecutor) { return ((ResizableExecutor)executor).getActiveCount(); } else { return -1; } } else { return -2; } }
// java.util.concurrent.ThreadPoolExecutor#getActiveCount /** * Returns the approximate number of threads that are actively * executing tasks. * * @return the number of threads */ publicintgetActiveCount(){ final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { int n = 0; for (Worker w : workers) if (w.isLocked()) ++n; return n; } finally { mainLock.unlock(); } }
获取
1 2 3 4 5 6 7 8 9 10 11
final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> names = server.queryNames(new ObjectName("Catalina:type=ThreadPool,*"), null); for (final ObjectName name : names) { System.out.println(name); final Object maxThreads = server.getAttribute(name, "maxThreads"); final Object currentThreadCount = server.getAttribute(name, "currentThreadCount"); final Object currentThreadsBusy = server.getAttribute(name, "currentThreadsBusy"); System.out.println("maxThreads = " + maxThreads); System.out.println("currentThreadCount = " + currentThreadCount); System.out.println("currentThreadsBusy = " + currentThreadsBusy); }