Java 如何同时调用多个方法?

Java 如何同时调用多个方法?,java,Java,我试图在一个方法中调用一些方法,并将它们存储在不同的列表中,以便在同一个方法中进一步使用。那么,有没有可能同时打电话给所有人呢。以下是代码: List<Student> students = studentMao.getAll(collegeId); List<Department> departments = departmentsMao.getByCollegeId(collegeId); List<College> colleges = collegeM

我试图在一个方法中调用一些方法,并将它们存储在不同的列表中,以便在同一个方法中进一步使用。那么,有没有可能同时打电话给所有人呢。以下是代码:

List<Student> students = studentMao.getAll(collegeId);
List<Department> departments = departmentsMao.getByCollegeId(collegeId);
List<College> colleges = collegeMao.getByUniversityId(universityId);
List students=studentMao.getAll(collegeId);
列表部门=部门mao.getByCollegeId(collegeId);
列出学院=collegeMao.getByUniversityId(universityId);

请提供任何建议

为了同时运行这三种方法,您可以使用
Executor
运行并发线程,并等待
Future
的结果。比如:

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();
Executor Executor=Executors.newFixedThreadPool(3);
未来学生未来=执行者.submit(()->return studentMao.getAll(collegeId));
Future departmentsFuture=executor.submit(()->return departmentsMao.getByCollegeId(collegeId));
Future collegesFuture=executor.submit(()->返回collegeMao.getByUniversityId(universityId));
List students=studentsFuture.get();
List departments=departmentsFuture.get();
列出学院=学院未来。获取();

Get将等待在另一个线程中运行的当前任务完成,因此当所有并发线程完成时,这段代码将完成。

您可以将它们包装在一个方法下并调用它…这取决于。您可以创建单独的线程,每个线程调用一个方法并存储结果。您所说的“同一时间”是什么意思?单一声明?相同的执行开始时间?意味着,所有方法调用应该同时执行或相同的执行开始时间。您到底想实现什么?为什么需要同时运行这些方法?