Heritrix之旅之ToeThread

来源:互联网 发布:java工程师前景 编辑:程序博客网 时间:2024/04/28 00:46
 

原文链接:http://www.cnblogs.com/MichaelYin/archive/2011/10/07/2200412.html

Heritrix是一个多线程的程序,里面使用工作线程来处理url,这些工作线程称为torThreads,这些toethread统一的被ToePool所管理,Toepool通过setSize方法来管理运行的toethread数目

view sourceprint?
00public void setSize(intnewsize) {
01targetSize = newsize;
02int difference = newsize - getToeCount();
03if (difference > 0) {
04// must create threads
05for (inti = 1; i <= difference; i++) {
06startNewThread();
07}
08} else{
09// must retire extra threads
10int retainedToes = targetSize;
11Thread[] toes = this.getToes();
12for (inti = 0; i < toes.length; i++) {
13if (!(toes[i] instanceof ToeThread)) {
14continue;
15}
16retainedToes--;
17if (retainedToes >= 0) {
18continue;// this toe is spared
19}
20// otherwise:
21ToeThread tt = (ToeThread) toes[i];
22tt.retire();
23}
24}
25}

ToeThread从Frontier中获取待处理的url,然后ToeThread对url进行一系列的处理,当所有的处理流程完成之后,调用Frontier的finished方法告知url抓取完毕,ToeThread重新获取新的url

这是ToeThread中的run方法的代码

view sourceprint?
00//从Frontier处获取待处理的url
01CrawlURI curi = controller.getFrontier().next();
02 
03synchronized(this) {
04continueCheck();
05setCurrentCuri(curi);
06}
07 
08//对url进行处理
09processCrawlUri();
10 
11setStep(STEP_ABOUT_TO_RETURN_URI);
12continueCheck();
13 
14//通知Frontier已经完成
15synchronized(this) {
16controller.getFrontier().finished(currentCuri);
17setCurrentCuri(null);
18}
19结合前面Frontier的图,可能看的就会明白一点了。
原创粉丝点击