Reputation: 275
I am trying to call some methods in a method and storing in different different List for further use in same method. So, is it possible to call all at same time. Here is codes:
List<Student> students = studentMao.getAll(collegeId);
List<Department> departments = departmentsMao.getByCollegeId(collegeId);
List<College> colleges = collegeMao.getByUniversityId(universityId);
Please any suggestion
Upvotes: 0
Views: 2466
Reputation: 745
In order to run all three methods concurrently you can use Executor
to run concurrent threads, and wait for the result thanks to Future
. Something like:
Executor executor = Executors.newFixedThreadPool(3);
Future<List<Student>> studentsFuture = executor.submit(() -> return studentMao.getAll(collegeId));
Future<List<Department>> departmentsFuture = executor.submit(() -> return departmentsMao.getByCollegeId(collegeId));
Future<List<College>> collegesFuture = executor.submit(() -> return collegeMao.getByUniversityId(universityId));
List<Student> students = studentsFuture.get();
List<Department> departments = departmentsFuture.get();
List<College> colleges = collegesFuture.get();
Get waits until the current task running in another threads finishes, so this piece of code will finish when all the concurrent threads have finished.
Upvotes: 2