Skip to content

Commit

Permalink
updated the executor service
Browse files Browse the repository at this point in the history
  • Loading branch information
theawesomechin committed May 31, 2021
1 parent 556a87d commit 4f0ea5d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Study/webcrawler-es-multithread.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import java.util.concurrent.ExecutorService;
import java.util.*;
/**
* // This is the HtmlParser's API interface.
* // You should not implement it, or speculate about its implementation
* interface HtmlParser {
* public List<String> getUrls(String url) {}
* }
*/
interface HtmlParser {
public List<String> getUrls(String url) {}
}
class Solution {
ExecutorService es;
public List<String> crawl(String startUrl, HtmlParser htmlParser) {
ExecutorService es = Executors.newFixedThreadPool(6, l -> {
Thread t = new Thread(l);
t.setDaemon(true);
return t;
});
es = Executors.newFixedThreadPool(6);

Set<String> visited = new HashSet();
Set<String> visited = new HashSet<>();

Queue<Future<List<String>>> q = new LinkedList<>();
String hostname = startUrl.indexOf('/', 7) == -1 ? startUrl : startUrl.substring(0,startUrl.indexOf('/', 7));
Expand All @@ -36,6 +38,10 @@ public List<String> crawl(String startUrl, HtmlParser htmlParser) {
}
}catch(Exception e){

}finally{
if(es != null){
es.shutdown();
}
}

return new ArrayList<String>(visited);
Expand Down

0 comments on commit 4f0ea5d

Please sign in to comment.