Heritrix之旅之ToeThread
2011-10-07 10:37 MichaelYin 阅读(413) 评论(0) 收藏 举报Heritrix是一个多线程的程序,里面使用工作线程来处理url,这些工作线程称为torThreads,这些toethread统一的被ToePool所管理,Toepool通过setSize方法来管理运行的toethread数目
public void setSize(int newsize) {
targetSize = newsize;
int difference = newsize - getToeCount();
if (difference > 0) {
// must create threads
for (int i = 1; i <= difference; i++) {
startNewThread();
}
} else {
// must retire extra threads
int retainedToes = targetSize;
Thread[] toes = this.getToes();
for (int i = 0; i < toes.length; i++) {
if (!(toes[i] instanceof ToeThread)) {
continue;
}
retainedToes--;
if (retainedToes >= 0) {
continue; // this toe is spared
}
// otherwise:
ToeThread tt = (ToeThread) toes[i];
tt.retire();
}
}
}
ToeThread从Frontier中获取待处理的url,然后ToeThread对url进行一系列的处理,当所有的处理流程完成之后,调用Frontier的finished方法告知url抓取完毕,ToeThread重新获取新的url
这是ToeThread中的run方法的代码
//从Frontier处获取待处理的url
CrawlURI curi = controller.getFrontier().next();
synchronized(this) {
continueCheck();
setCurrentCuri(curi);
}
//对url进行处理
processCrawlUri();
setStep(STEP_ABOUT_TO_RETURN_URI);
continueCheck();
//通知Frontier已经完成
synchronized(this) {
controller.getFrontier().finished(currentCuri);
setCurrentCuri(null);
}
结合前面Frontier的图,可能看的就会明白一点了。
浙公网安备 33010602011771号