Heritrix之旅之ToeThread

 

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

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

00public void setSize(int newsize) {
01targetSize = newsize;
02int difference = newsize - getToeCount();
03if (difference > 0) {
04// must create threads
05for (int i = 1; i <= difference; i++) {
06startNewThread();
07}
08} else {
09// must retire extra threads
10int retainedToes = targetSize;
11Thread[] toes = this.getToes();
12for (int i = 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方法的代码

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的图,可能看的就会明白一点了。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值