首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java上“阻塞”和"TIMED_WAITING“的区别

java上“阻塞”和"TIMED_WAITING“的区别
EN

Stack Overflow用户
提问于 2014-09-16 10:08:10
回答 1查看 10K关注 0票数 3

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

代码语言:javascript
复制
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)

我的代码:

代码语言:javascript
复制
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();
    }
}
}

中调用该方法

代码语言:javascript
复制
public void check() {
    for (final JdbcTemplate jdao : jdbcTemplates) {
        threadPoolExecutor.execute(new Runnable(){
            public void run() {
                checkMaster(jdao);
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-09-16 16:26:32

您说得对,方法Thread中的Thread.sleep的线程状态应该是TIMED_WAITING

引用权威来源

public static final Thread.State TIMED_WAITING 具有指定等待时间的等待线程的线程状态。由于调用具有指定正等待时间的下列方法之一,线程处于计时等待状态:

  • Thread.sleep
  • 带有超时的Object.wait
  • 带有超时的Thread.join
  • LockSupport.parkNanos
  • LockSupport.parkUntil

我在1.61.8的范围内测试了几个Java版本(Oracle的实现),所有这些都显示了在Thread.sleep中使用状态TIMED_WAITING报告线程的正确行为。

考虑Thread.sleep()也很重要

…线程不会失去任何监视器的所有权。

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

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

知道您所使用的jstack的哪个版本作为输出有一个与我不同的格式也是有用的。也许是打印错误状态…的工具

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25865972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档