My j2ee application is running on tomcat server. I am monitoring the server status (number of live threads) through Jconsole. When the number of thread reaches 3000+ the tomcat is crashing. What I observed that even after thread execution is completed the active count still reamins same once the new thread is created. So ultimately the count goes on increasing and the tomcat crashes.
Does JConsole allow you to peer into a thread to see what it is doing? Sounds like you are in an infinite loop or blocking on something that never occurs / takes a long time to occur. You will need to troubleshoot some more to see what code the threads are executing.
No JConsole only shows the Active thread count in an application and memory usage. Actually the code executed by thread is completed. I can trace it through loggers in the code. Even for the each thread the run() method is completing its execution still the threads are active . Even runtime.gc() or System.gc() doesnt work.
Yes you are right that I can avoid creating new thread if the thread count is high. But if I do that how can I serve user request? Because the active threads in tomcat are never garbage collected unless its restarted.
Author and all-around good cowpoke
Joined: Mar 22, 2000
Ganesh Gore wrote:Yes you are right that I can avoid creating new thread if the thread count is high. But if I do that how can I serve user request? Because the active threads in tomcat are never garbage collected unless its restarted.
Like I said, Tomcat handles creating the Threads to process requests - once created these Threads stay alive in a ThreadPool - the next available Thread in the pool is assigned to handle the next request.
Your application should NOT be creating any Threads unless you have a special situation and really really know what you are doing.
Ganesh Gore wrote:Using a "ThreadPoolExecutor " can help in this case?
I think you are missing the point. You should not be asking how to create more threads. You need to be asking why your threads aren't coming to an end/being reused.
So, a few questions:
1) Are you creating your own threads?
2) Are the 3000+ threads you are seeing created by you or are they Tomcat request threads?
You must be able to answer those questions. The names of the Threads will probably help you.
The next question you have to ask is: Why are those threads not being re-used? In order to get an answer to that you need to find out what they are doing, and for that you need stack traces. If you can't figure out how to get stack traces from JConsole, then get another tool which does. But according to the JConsole documentation (see: this picture of the Thread tab) it looks like JConsole does give you that infortmation (select the thread name in bottom left corner to get its stack trace in bottom right corner). If you look at the stack trace it should tell you what your threads are waiting on or are doing.
If you know that the threads are coming from Tomcat, and you know that they aren't blocking in code you wrote (by looking at the stack trace) then this could be a configuration error. As has been said, Tomcat keeps a bunch of Threads alive for hosting requests, and these threads are configured in the <Executor> element in your configuration file. Make sure the maxThreads is set to something reasonable, and maybe ensure minSpareThreads is set to a reasonable size so as to ensure the thread count shrinks when there are fewer request than threads (these values can also be set in the default HTTP Connector element as well).
If you know the threads are coming from threads you start, or that Tomcat threads are sitting in code you wrote you have to diagnose your own code to see why that is happening.