gpt4 book ai didi

java - RUNNABLE 线程在 native 方法中可能不消耗 CPU?

转载 作者:行者123 更新时间:2023-11-29 02:57:51 25 4
gpt4 key购买 nike

当对大部分处于空闲状态的 Tomcat 服务器进行线程转储时,很多线程可能会以 RUNNABLE 状态显示,如下所示:

"http-bio-8443-exec-21975" daemon prio=10 tid=0x00007f6f6406c000 nid=0x222a runnable [0x00007f6f156ae000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:516)
at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:501)
at org.apache.coyote.http11.Http11Processor.setRequestLineReadTimeout(Http11Processor.java:173)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:924)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
- locked <0x00000007cadcd3f8> (a org.apache.tomcat.util.net.SocketWrapper)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)

我对源代码的解释是这个线程正在等待(超时)来自 HTTP keepalive 连接的更多数据。因此,即使线程是 RUNNABLE,它也不会消耗 CPU。

Thread.State RUNNABLE javadoc说:

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.

因此在这种情况下,其他资源将是 I/O 而不是 CPU。

在其他问题中Java socketRead0 Issue , Geoff答案:

I believe that when you are in a Java native method, the stack trace will say RUNNABLE even if the call is actually blocked waiting for some event. In essence, I don't believe Java has any way of knowing what a native method is actually doing, so it flags these calls as RUNNABLE. I have seen this with socketRead0() and socketAccept() -- both of which typically block.

我得出了类似的结论,我想在这个专门的问题中验证这个解释,即:

如果我想通过查看 RUNNABLE 线程来分析 CPU 消耗,我可能必须通过非常仔细地查看其源代码来排除本地方法中的线程?

重点是它不像只查看线程的状态那么容易,而是必须深入研究源代码并开始猜测特定 native 方法可能在做什么(甚至查看其 C 或 C++ 源代码)。

最佳答案

关于 It's OK to Ask and Answer Your Own Questions 的行,我的回答是肯定的,这可能是必要的,并且对到目前为止遇到的 native 方法进行一些总结可能会有用。

关于java - RUNNABLE 线程在 native 方法中可能不消耗 CPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com