分析java堆栈跟踪,哪些线程正在运行
我正在分析 java 中的堆栈跟踪转储,但不知道如何判断哪些线程正在实际运行。在我看来,线程要么正在运行,要么正在等待锁,要么正在休眠,要么等待被调度。
在我的堆栈跟踪中,我看到以下状态
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)
首先,这些状态到底意味着什么?
其次,我有 11 个可运行的线程,其中两个正在等待某个条件。这是否意味着 9 个线程同时运行?这似乎不可能,因为我的机器没有那么多核心。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
术语“可运行”可能并不意味着“当前正在运行”,而是“处于运行状态”。您所回避的差异是,在任何给定时间可以运行的最大线程数等于您的核心数。
要更详细地理解它,您需要知道其他两个的含义。
WAITING
意味着线程已在某个对象上挂起。在这里,当前线程当前正在 obj 的监视器上
WAITING
。这将迫使该线程挂起,操作系统可以在其中调度另一个线程运行。TIMED_WAITING
只是将线程挂起一段时间。因此,如果线程没有
等待
或睡眠
,它就会被视为运行
。从文档中获得更多见解。
我认为最后一部分就是您正在寻找的内容。从物理上讲,操作系统并行运行的线程数量不能多于内核数量。
所有 9 个线程都可以同时运行,但只有
n
会并行运行(n
是核心数量)。Condition.await
也是如此编辑:
对不起,丰富 - 应该链接我的资源 http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html#RUNNABLE
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.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
wait
ing orsleep
ing it is consideredrun
ning.To give more insights from the documentation.
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
看看这个页面是否对您有帮助
http://download.oracle.com/javase /6/docs/api/java/lang/Thread.State.html
回答关于核心数与线程数的第二个问题。一个核心可以同时运行多个线程。因此 # 个线程 >= # 个核心
每个线程的完整堆栈将使您更好地了解每个线程正在做什么。如果应用程序中当前没有任何工作正在完成,则许多应用程序将处于等待状态,读取以接受其下一个工作。
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.