zoukankan      html  css  js  c++  java
  • Java 线程内 递归 Bug 一例

    一个线程的run方法里使用递归方法,出了Bug。

    private boolean ispass(String creationId){
    List<Map> maps =creationService.getCreationById(creationId);
    if(maps != null && maps.size()>0){
    Map m = maps.get(0);
    String state= (String) m.get("STATE");
    if(state != null && state.equals("0")){
    return true;
    }else{
    try {
    if(falg == 10){
    return false;
    }
    Thread.sleep(1000 * 60);
    falg++;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
    }
    ispass(creationId);
    }
    }else{
    return false;
    }
    return false;
    }

    同事的解决办法,修改代码通过抛出异常的方式,也算解决了。当然了我不认可 。:)

    private boolean ispass(String creationId) throws SecussException{
    List<Map> maps =creationService.getCreationById(creationId);
    if(maps != null && maps.size()>0){
    Map m = maps.get(0);
    String state= (String) m.get("STATE");
    if(state != null && state.equals("0")){
    throw new SecussException("ok");
    }else{
    try {
    if(falg == 10){
    return false;
    }
    Thread.sleep(1000 * 60);
    falg++;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
    }
    ispass(creationId);
    }
    }else{
    return false;
    }
    return false;
    }

    我的解决办法:调试代码,发现问题根源,该返回的地方,没返回;不该返回的地方,返回了错误的值。

    private boolean ispass(String creationId){
    List<Map> maps =creationService.getCreationById(creationId);
    if(maps != null && maps.size()>0){
    Map m = maps.get(0);
    String state= (String) m.get("STATE");
    if(state != null && state.equals("0")){
    return true;
    }else{
    try {
    if(falg == 10){
    return false;
    }
    Thread.sleep(1000 * 60);
    falg++;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
    }
    return ispass(creationId);
    }
    }else{
    return false;
    }
    }

  • 相关阅读:
    机器人
    昨天拿钥匙了
    Linux挂NTFS格式USB硬盘
    漫游在首都
    RHEL+WAS6部署风波
    移动电话国内漫游通话费上限评估用户意见网上调查
    WebSphere fixes
    我太强悍了
    NO SUBJECT
    pku3617 Best Cow Line
  • 原文地址:https://www.cnblogs.com/rgqancy/p/5329689.html
Copyright © 2011-2022 走看看