I have a for loop in which i make call to db to get some values of a student. Now for loop takes a lot of time. So thought to replace that with executor service with future objects. At the start of function i made calls to db using executor.submit() method.
I am just not sure how to find the future objects returned is for that particular student which i can use for further steps. Please find below the code
Function below is getting called from a for loop which overall takes lot of time since getting called by each Student:
public void getDbDetails(String student){
//calling db
if(dbDetails.studentName = "XYZ"){
//function based on student ID
}
}
Using Executor Framework:
public void getStudentData(List<String> students){
final List<Future<?>> futures = new ArrayList<>();
for(String student: students){
Future<?> future = executor.submit(() ->{
//db call
});
futures.add(future);
}
}
public void getDbDetails(String student){
//In this case how would i know whether future has returned details for this particular student??
if(dbDetails.studentName = "XYZ"){
//function based on student ID
}
}
Is it possible to use future.get() and create a map based on the studentId from future object and then use that map in function getDbDetails() to fetch particular student information and if future has not returned anything by that time then use Thread.wait();? Is this approach right or anything else can be used as optimum solution?