I'm really new to concurrency in java.
This is the ExecutorService code:
ExecutorService pool = Executors.newFixedThreadPool(7);
List<Future<Hotel>> future = new ArrayList<>();
List<Callable<Hotel>> callList = new ArrayList<>();
for (int i = 0; i <= diff; i++) {
String str = "2013-" + (liDates.get(i).get(Calendar.MONTH) + 1) + "-" + liDates.get(i).get(Calendar.DATE);
callList.add(new HotelCheapestFare(str));
}
future = pool.invokeAll(callList);
for (int i = 0; i < future.size(); i++) {
System.out.println("name is: " + future.get(i).get().getName());
}
Now my question is, why can't i write it like:
List<Hotel> result=new ArrayList<>();
for (int i = 0; i <= diff; i++) {
String str = "2013-" + (liDates.get(i).get(Calendar.MONTH) + 1) + "-" + liDates.get(i).get(Calendar.DATE);
result.add(new HotelCheapestFare(str));
}
for (int i = 0; i < result.size(); i++) {
System.out.println("name is: " + result.get(i).getName());
}
I know that ExecutorService.invokeAll() executes all the Callable<T> in the thread pool parallely, but
for (int i = 0; i <= diff; i++) {
String str = "2013-" + (liDates.get(i).get(Calendar.MONTH) + 1) + "-" + liDates.get(i).get(Calendar.DATE);
callList.add(new HotelCheapestFare(str));
}
this part of the code is not being run in the thread pool right? then what even is the use of using thread pool here