gpt4 book ai didi

java - java上 "BLOCKED"和 "TIMED_WAITING"的区别

转载 作者:行者123 更新时间:2023-12-01 12:31:51 25 4
gpt4 key购买 nike

我们的 tomcat 没有响应任何请求。当我使用“jstack pid”打印堆栈信息时,我得到了以下信息。我发现它在“Thread.sleep(long)”上被阻止。我认为应该是“TIMED_WAITING”。为什么?

Thread 24836: (state = BLOCKED)
- java.lang.Thread.sleep(long) @bci=0 (Compiled frame; information may be imprecise)
- com.lagou.base.proxy.MasterTemplateContainer.checkMaster(org.springframework.orm.hibernate3.HibernateTemplate) @bci=128, line=221 (Compiled frame)
- com.lagou.base.proxy.MasterTemplateContainer.access$1(com.lagou.base.proxy.MasterTemplateContainer, org.springframework.orm.hibernate3.HibernateTemplate) @bci=2, line=185 (Interpreted frame)
- com.lagou.base.proxy.MasterTemplateContainer$2.run() @bci=8, line=134 (Interpreted frame)
- java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) @bci=95, line=1145 (Interpreted frame)
- java.util.concurrent.ThreadPoolExecutor$Worker.run() @bci=5, line=615 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=744 (Interpreted frame)


Thread 24835: (state = BLOCKED)
- java.lang.Thread.sleep(long) @bci=0 (Compiled frame; information may be imprecise)
- com.lagou.base.proxy.MasterTemplateContainer.checkMaster(org.springframework.jdbc.core.JdbcTemplate) @bci=128, line=177 (Compiled frame)
- com.lagou.base.proxy.MasterTemplateContainer.access$0(com.lagou.base.proxy.MasterTemplateContainer, org.springframework.jdbc.core.JdbcTemplate) @bci=2, line=141 (Interpreted frame)
- com.lagou.base.proxy.MasterTemplateContainer$1.run() @bci=8, line=125 (Interpreted frame)
- java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) @bci=95, line=1145 (Interpreted frame)
- java.util.concurrent.ThreadPoolExecutor$Worker.run() @bci=5, line=615 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=744 (Interpreted frame)

我的代码:

private void checkMaster(HibernateTemplate hdao) {
int loopErrors = errorTimes;
int loopSuccess = successTimes;
while(true) {
boolean success = true;
try {
if(hdao == null){
success = false;
continue;
}
success = checkMasterStatusOnce(hdao);//try access database to get the status
} catch (Exception e1) {
logger.error("",e1);
success = false;
}

try {
Thread.sleep(checkInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

该方法被调用

public void check() {
for (final JdbcTemplate jdao : jdbcTemplates) {
threadPoolExecutor.execute(new Runnable(){
public void run() {
checkMaster(jdao);
}
});
}
}

最佳答案

你是对的,Thread的线程状态方法内部Thread.sleep应该是TIMED_WAITING .

引用authoritative source :

public static final Thread.State TIMED_WAITING

Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

我测试了 1.6 范围内的多个 Java 版本(Oracle 的实现)至1.8 ,包含在内,并且都显示了 Thread.sleep 内报告线程的正确行为。与国家TIMED_WAITING .

考虑the following statement about Thread.sleep() 也很重要:

… The thread does not lose ownership of any monitors.

因此,由于线程不会失去监视器的所有权,因此它不会重新获取监视器。所以它不应该在 Thread.State.BLOCKED 中.

因此,您要么使用不同的 JVM/JRE 实现,要么使用修改后的 Thread类,也许它已在运行时通过 Instrumentation 进行了修改。无论哪种情况,您在问题中提供的信息都不足以进一步缩小您的问题范围。

了解 jstack 的版本也很有用。你使用的输出格式与我得到的格式不同。也许是打印状态的工具错误......

关于java - java上 "BLOCKED"和 "TIMED_WAITING"的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25865972/

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