Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the cause for waiting/sleeping threads

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?

like image 532
Timo Ernst Avatar asked Nov 10 '11 21:11

Timo Ernst


People also ask

Does sleeping on a thread consume resources?

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.

Can sleep () method causes another thread to sleep?

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.

What is AbstractQueuedSynchronizer ConditionObject?

public class AbstractQueuedSynchronizer.ConditionObject extends Object implements Condition, Serializable. Condition implementation for a AbstractQueuedSynchronizer serving as the basis of a Lock implementation.

How do you wait in a thread?

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.


2 Answers

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.

like image 185
ptyx Avatar answered Sep 16 '22 17:09

ptyx


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.

like image 28
John Vint Avatar answered Sep 18 '22 17:09

John Vint