2 adrian lyl ADRIAN_lyl 于 2017.01.17 11:46 提问

求助 web项目出现了多线程假死问题

下面的代码会出现多线程假死问题 请问有什么毛病没???
public class FetchHenandaily implements Runnable {
private static final Log logger = LogFactory.getLog(FetchHenandaily.class);
private static final String HENANURL = "http://api.henandaily.cn/v1/content/getcategorylist?user_id=0&token=8de8af6f01e8a9b8b2a649a9&cat_id=content_";
private static final List COLUMNS = Arrays.asList(new Integer[]{Integer.valueOf(23), Integer.valueOf(24), Integer.valueOf(8), Integer.valueOf(9), Integer.valueOf(112), Integer.valueOf(128), Integer.valueOf(129), Integer.valueOf(250), Integer.valueOf(27), Integer.valueOf(246), Integer.valueOf(126), Integer.valueOf(127), Integer.valueOf(131), Integer.valueOf(133), Integer.valueOf(134), Integer.valueOf(135), Integer.valueOf(136), Integer.valueOf(109), Integer.valueOf(110), Integer.valueOf(137), Integer.valueOf(138), Integer.valueOf(139), Integer.valueOf(140), Integer.valueOf(142), Integer.valueOf(143), Integer.valueOf(113), Integer.valueOf(144), Integer.valueOf(145), Integer.valueOf(146), Integer.valueOf(148)});
private static final String KEY = "henandaily_info";

public FetchHenandaily() {
}

private void beginHenadaily() {
    logger.info("-------begin henandaily begin----------");
    String url = "";
    String backstr = "";
    HenanArticle arts = null;
    boolean existurl = false;
    String contentLink = "";
    ArticleInfo arto = null;
    String content = "";
    Iterator var9 = COLUMNS.iterator();

    while(var9.hasNext()) {
        Integer s = (Integer)var9.next();
        long cumment = System.currentTimeMillis();
        logger.info("s:" + s);
        url = "http://api.henandaily.cn/v1/content/getcategorylist?user_id=0&token=8de8af6f01e8a9b8b2a649a9&cat_id=content_" + s + "&offset=0&num=0&_=" + cumment;
        logger.info("--url is---" + url);
        backstr = HttpRequestProxy.doGet(url, (Map)null);
        arts = (HenanArticle)JSON.parseObject(backstr, HenanArticle.class);
        Iterator var13 = arts.getInfo().iterator();

        while(var13.hasNext()) {
            HenanInfo info = (HenanInfo)var13.next();
            if(info.getUrl().startsWith("http://www.henandaily.cn/")) {
                contentLink = info.getUrl();
                logger.info("contentLink:" + contentLink);
                existurl = RedisApi.getInstance().existSetsVlaue("henandaily_info", contentLink);
                if(!existurl) {
                    arto = new ArticleInfo();
                    content = ParseHtml.parseHenandailyContent(HttpRequestProxy.doGet(contentLink, (Map)null));
                    if(!StringUtils.isBlank(content)) {
                        RedisApi.getInstance().setSets("henandaily_info", contentLink);
                        arto.setContent(content);
                        arto.setTitle(info.getTitle());
                        arto.setIsSens(0);
                        arto.setProvince(16);
                        arto.setSid(999999996);
                        arto.setSource("河南日报金水河");
                        arto.setSourceType(6);
                        arto.setUrl(contentLink);
                        arto.setPubTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                        arto.setSpiderTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                        arto.setSummary(info.getBrief());
                        RedisApi.getInstance().setListsValue(ResoucesUtils.getRedisKey(), JSON.toJSONString(arto));
                    }
                }
            }
        }
    }

}

public void run() {
    this.beginHenadaily();
    logger.info("-------henandaily is over--------");
}

}

1个回答

baij1230
baij1230   2017.01.18 21:10

iterator本身并不是线程安全的,需要同步块

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!