I noticed that my java application (runs on tomcat6) spawns a lot of threads which do not terminate.
So I created a thread dump and noticed that there are tons of threads waiting, like this:
"pool-1-thread-22" prio=5 tid=101b4b000 nid=0x127122000 waiting on condition [127121000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <6c340cee0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1987) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:399) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:947) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) at java.lang.Thread.run(Thread.java:680) Locked ownable synchronizers: - None
Now the question is: WHAT are these threads waiting for? I have suspect class which seems to spawn these threads but I don't know what exactly is making these threads stuck.
Is there anything I can do to find the cause for this except for tearing the class apart line by line and keep monitoring thread behavior?
For most cases, the resources consumed by a sleeping thread will be its stack space. Using a 2-threads-per-connection-model, which I think is similar to what you're describing, is known to cause great scalability issues for this very reason when the number of connections grow large.
Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t. sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
public class AbstractQueuedSynchronizer.ConditionObject extends Object implements Condition, Serializable. Condition implementation for a AbstractQueuedSynchronizer serving as the basis of a Lock implementation.
wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor.
On tomcat, they're usually request worker threads waiting for someone to connect. Nothing to worry about. They're ready to handle those 100 users connecting at once to your server.
Those threads are part of a ThreadPool. More specificaly java.util.concurrent.ThreadPoolExecutor. The thread is waiting on a Runnable/Callable to be submitted to the pool. For example
ExecutorService e = Executors.newFixedThreadPool(10);
Will create 10 threads that will sit an wait until
e.submit(new Runnable(){ public void run(){ ...} });
Then one thread will be notified and invoke that runnable. What they are being used for I cannot tell. You'll have to find out what started the thread pool. Maybe its handling client requests to the application server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With