分析java堆栈跟踪,哪些线程正在运行

发布于 2024-10-29 07:57:58 字数 459 浏览 3 评论 0原文

我正在分析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-11-05 07:57:58

术语“可运行”可能并不意味着“当前正在运行”,而是“处于运行状态”。您所回避的差异是,在任何给定时间可以运行的最大线程数等于您的核心数。

要更详细地理解它,您需要知道其他两个的含义。 WAITING 意味着线程已在某个对象上挂起。

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

在这里,当前线程当前正在 obj 的监视器上WAITING。这将迫使该线程挂起,操作系统可以在其中调度另一个线程运行。

TIMED_WAITING 只是将线程挂起一段时间。

因此,如果线程没有等待睡眠,它就会被视为运行

从文档中获得更多见解。

可运行线程的线程状态。一个
处于可运行状态的线程是
在Java虚拟机中执行
但它可能正在等待其他
来自操作系统的资源
例如处理器。

我认为最后一部分就是您正在寻找的内容。从物理上讲,操作系统并行运行的线程数量不能多于内核数量。
所有 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.

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

滴情不沾 2024-11-05 07:57:58

看看这个页面是否对您有帮助
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文