I have created a dedicated thread pool to handle specific HTTP requests in Glassfish v3. I would like to get the number of inactive threads (free- not running) at any given point of time as I need to throttle the HTTP requests depending on the availability of worker threads. Is there a exposed API to get this? I don't want to submit the request to the thread pool unless threads are available.
To answer your question: As per best of my knowledge there is so no such API which can give you handy details you require, so I think you have to compute it yourself.
For solution part: java.lang.management
package should help you.
java.lang.management.ManagementFactory
The ManagementFactory class is a factory class for getting managed beans for the Java platform.
You can consider using ManagementFactory
which lets you get details from the JVM through exposed Java managed beans. For your case, you can use ThreadMXBean
which will let you get all JVM thread information.
java.lang.management.ThreadMXBean
The management interface for the thread system of the Java virtual machine.
Read ThreadMXBean
documentation and API thoroughly to understand it well, and I think in the end you can use java.lang.management.ThreadInfo
object to get all the info you need.
java.lang.management.ThreadInfo
java.lang.management.ThreadInfo
gets you a lot of Thread details as listed below. I don't think any other Java class can give this much info about Thread.
Below is a sample I have created for you, please note this is to get you started and a full-fledged solution cannot be provided, so please do more research about it, but I think would be helpful.
I am getting all thread states and printing them, so you can do IF-ELSE and then prepare a list of something based on state etc. and then take necessary action.
Hope this helps!
private static void getThreadInfo() {
System.out.println("Started");
ThreadMXBean managementFactory = ManagementFactory.getThreadMXBean();
long threadIds[] = managementFactory.getAllThreadIds();
for (int i = 0; i < threadIds.length; i++) {
ThreadInfo info = managementFactory.getThreadInfo(threadIds[i]);
System.out.println("Thread name = " + info.getThreadName() + " Thread id = " + info.getThreadId() + " Thread state = " + info.getThreadState());
}
System.out.println("#############");
System.out.println(Thread.currentThread().getAllStackTraces());
}
Output:
Started
Thread name = Attach Listener Thread id = 5 Thread state = RUNNABLE
Thread name = Signal Dispatcher Thread id = 4 Thread state = RUNNABLE
Thread name = Finalizer Thread id = 3 Thread state = WAITING
Thread name = Reference Handler Thread id = 2 Thread state = WAITING
Thread name = main Thread id = 1 Thread state = RUNNABLE
#############
{Thread[Finalizer,8,system]=[Ljava.lang.StackTraceElement;@1748ba4, Thread[Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@7bd86d, Thread[main,5,main]=[Ljava.lang.StackTraceElement;@bdff3b, Thread[Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@1bf8a41, Thread[Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@dd841}