当下载第一个URL时(一般是网站主页),如果等待时间过长,那么其他线程要么会认为网站已下载完而结束,要么会在下面标*代码处抛出NullPointerException, 很少能够存活下来。

else if(queueSize() == 0)       /* queueSize()已经被同步 */
{
    break;
}
URLToDownload nextURL;
synchronized(queue)
{
    nextURL = queue.getNextInQueue();
    downloadsInProgress++;
}
synchronized(urlsDownloading)
{
    urlsDownloading.add(nextURL);
}
int newDepth = nextURL.getDepth() + 1; **********************


估计可能是线程交叉了,还没来得及同步就跑到后面去执行getDepth()了。
nextURL = queue.getNextInQueue();后面加上判断就OK了:

synchronized(queue)
{
    nextURL = queue.getNextInQueue();
    if(nextURL == null)
    {
        continue;
    }

    downloadsInProgress++;
}




版权所有 罗明
posted on 2006-02-16 14:10 罗明 阅读(1088) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: