-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebCrawler.java
68 lines (56 loc) · 1.4 KB
/
WebCrawler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package org.blueocean;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
public class WebCrawler {
public void driver(){
BlockingDeque urls = new LinkedBlockingDeque(10);
ExecutorService service = Executors.newFixedThreadPool(10);
List<Crawler> runners = new ArrayList<Crawler>();
for(int i=0; i<10; i++){
Crawler c = new Crawler(urls, runners);
runners.add(c);
service.submit(c);
}
}
private static URL[] crawl(URL url) {
// TODO Auto-generated method stub
return null;
}
public static class Crawler implements Runnable{
BlockingDeque<URL> task;
List<Crawler> runners;
public Crawler(BlockingDeque urls, List<Crawler> rs){
this.task = urls;
this.runners = rs;
}
public URL getTask(){
return task.poll();
}
@Override
public void run() {
URL url;
try {
url = (URL)task.poll();
if(url==null){
for(Crawler c : runners){
if(c.getTask()!=null){
url = c.task.take();
}
}
}
URL[] urls = crawl(url);
for(URL u:urls)
task.put(u);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}