當(dāng)下載第一個(gè)URL時(shí)(一般是網(wǎng)站主頁(yè)),如果等待時(shí)間過長(zhǎng),那么其他線程要么會(huì)認(rèn)為網(wǎng)站已下載完而結(jié)束,要么會(huì)在下面標(biāo)*代碼處拋出
NullPointerException, 很少能夠存活下來。
else if(queueSize() == 0) /* queueSize()已經(jīng)被同步 */
{
break;
}
URLToDownload nextURL;
synchronized(queue)
{
nextURL = queue.getNextInQueue();
downloadsInProgress++;
}
synchronized(urlsDownloading)
{
urlsDownloading.add(nextURL);
}
int newDepth = nextURL.getDepth() + 1; **********************
估計(jì)可能是線程交叉了,還沒來得及同步就跑到后面去執(zhí)行g(shù)etDepth()了。
在
nextURL = queue.getNextInQueue();后面加上判斷就OK了:
synchronized(queue)
{
nextURL = queue.getNextInQueue();
if(nextURL == null)
{
continue;
}
downloadsInProgress++;
}
版權(quán)所有 羅明