I have a program which is able to get html document from the list of sites one by one and then parse it.
ArrayList<String> links = new ArrayList<>();
for(String link : links) {
try {
Document doc = Jsoup.connect(link).get();
getInfo(doc);
}catch (IOException e) {
e.printStackTrace();
}
}
Problem is, that it's taking to long to get html documents like, site1 then site2 and site3 ..
My question is, is it possible to make that this code would connect to 5 links at same time and then parse them instead of one by one.
Yes.
Probably the most simple one with Java8 is to use a parallel stream
ArrayList<String> links = new ArrayList<>();
links.parallelStream().forEach(link -> {
try {
Document doc = Jsoup.connect(link).get();
getInfo(doc);
}catch (IOException e) {
e.printStackTrace();
}
});
Of course there are many alternative ways, including threads, executor pools, etc - just use google searching for concurrency, threads and whatnot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With