• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Live thread count remain same in tomcat server, unless restarted.

 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

Please help.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this was my problem I would use the Management App to look at the status of the request Threads.

Tomcat creates a thread pool to handle requests, once created these threads never die, they just go back to the pool after handling a request.

If the management app shows that a particular request has been running for a long time, it may be blocked like Steve said.

If you are seeing more Threads than the configuration calls for, why does your application create Threads?

Bill
 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill

 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a "ThreadPoolExecutor " can help in this case?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thank you Steve for your valuable reply. Will soon investigate the code and will get back to you soon.
 
Ganesh Gore
Greenhorn
Posts: 24
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to check the stack trace through JConsole. Even after execution is finished it shows runnable state.


Name: Thread-3877
State: RUNNABLE
Total blocked: 0 Total waited: 0

Stack trace:
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(Unknown Source)
com.trilead.ssh2.crypto.cipher.CipherInputStream.fill_buffer(CipherInputStream.java:41)
com.trilead.ssh2.crypto.cipher.CipherInputStream.internal_read(CipherInputStream.java:52)
com.trilead.ssh2.crypto.cipher.CipherInputStream.getBlock(CipherInputStream.java:79)
com.trilead.ssh2.crypto.cipher.CipherInputStream.read(CipherInputStream.java:108)
com.trilead.ssh2.transport.TransportConnection.receiveMessage(TransportConnection.java:232)
com.trilead.ssh2.transport.TransportManager.receiveLoop(TransportManager.java:670)
com.trilead.ssh2.transport.TransportManager$1.run(TransportManager.java:469)
java.lang.Thread.run(Unknown Source)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic