I have some task to aggregate some info from multiple links in most effective way, using multithreading. The links are at some array. By now, i have something like this solution:
Arrays.stream(link).parallel().forEach(link -> {
try {
String result = doSomeJobWithLink(link);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
});
And it works pretty good (job has done for 2 secs).
But I wanna not to print result in my try block, but collect results in some list (or other collection), so i did it this way:
List<String> resultList = Collections.synchronizedList(new ArrayList<>());
Arrays.stream(link).parallel().forEach(link -> {
try {
String result = doSomeJobWithLink(link);
resultList.add(result);
} catch (IOException e) {
e.printStackTrace();
}
});
resultList.forEach(System.out::println);
But it's took about 5-8 sec's instead of two. Can I speed it up someway?