richs
richs

Reputation: 4769

Analysis of java stack trace, what threads are running

I'm analyzing stack trace dumps in java and don't know how I can tell which threads are actually running. The way I see it a thread is either running, waiting on a lock, sleeping or waiting to be scheduled.

In my stack trace I see the following states

java.lang.Thread.State: WAITING (on object monitor)
java.lang.Thread.State: RUNNABLE
java.lang.Thread.State: TIMED_WAITING (sleeping)
java.lang.Thread.State: WAITING (parking)
java.lang.Thread.State: TIMED_WAITING (parking)

First, what exactly do these states mean?

Second, I have 11 threads that are runnable, two of which are waiting on a condition. Does that mean 9 threads are running simultaneously? It doesn't seem possible since my machine doesn't have that amount of cores.

Upvotes: 1

Views: 5071

Answers (2)

John Vint
John Vint

Reputation: 40266

The term Runnable may not mean 'currently running' but rather 'is in a state of running'. The differences as you eluded to is that the maximum number of threads that can be running at any given time is equal to your number of cores.

To understand it in more details, you would need to know what the other two mean. WAITING would mean that a thread has been suspended on some object.

while(<condition holds>)
    obj.wait()

Here, the current thread is currently WAITING on obj's monitor. This would force that thread to become suspended in which the OS can schedule another thread to run.

TIMED_WAITING is simply suspending a thread for a certain amount of time.

So if a thread isn't waiting or sleeping it is considered running.

To give more insights from the documentation.

Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.

That last part is what I think you are looking for. Physically the OS cannot run more threads in parallel than the amount of the cores.
All 9 of your threads may run concurrently but only n will run in parallel (n being the number of cores).

Similar is true for Condition.await

Edit:

Sorry richs - Should've linked my resource http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html#RUNNABLE

Upvotes: 6

Sean
Sean

Reputation: 7747

See if this Page helps you http://download.oracle.com/javase/6/docs/api/java/lang/Thread.State.html

To answer your second question about the number of cores vs threads. A core can have multiple threads running on it at once. Therefore # threads >= # cores

The full stack from each thread will give you a better understanding of what each is doing. If there is no work currently being done in the application, many will be sitting in a waiting state, read to accept its next job.

Upvotes: 0

Related Questions