gpt4 book ai didi

java - 使用 ExecutorService 在具有通过循环传递的不同参数的类中同时执行方法时出现问题

转载 作者:行者123 更新时间:2023-12-02 01:31:18 26 4
gpt4 key购买 nike

我正在尝试编写一个程序来处理大约一百万名大量员工的操作。我正在使用 ExecutorService 并行化操作,以在池大小为 1000 的员工循环中创建线程。(我有 24 核(48 逻辑核)intel cpu 和 128gb ram 服务器)。我的操作包括许多数据库访问,这就是我使用大小为 1000 的线程池的原因。

我的目标是处理对员工的操作,而主线程应该等待所有其他线程完成作业,然后返回处理结果。问题是主线程在创建线程后立即返回。

Servlet 代码:

public class EmployeeProcess extends HttpServlet {

protected void doGet(....) {
employeeDAO.executePrepareReport();
}

}

第二类:


public class EmployeeDAOImpl implements EmployeeDAO {

public void executePrepareReport() {
ExecutorService executorService = Executors.newFixedThreadPool(1000);
// method reference introduced in Java 8
for(Employee employee : comp.listOfEmp) {

executorService.submit(new Runnable() {
public void run() {
prepareEmpReport(employee);
}
});
}
executorService.shutdown();
//executorService.awaitTermination();
}

@Override
public void prepareEmpReport(Employee employee) {

// process employee report with database accesses

}

}

请建议代码更正或其他有效的方法

最佳答案

有不同的方法来解决这个问题。

两种常见的方法是:

  • 倒计时锁
  • ExecutorService.invokeAll
<小时/>

您可以使用 CountDownLatch :

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

<小时/>

您可以使用方法invokeAll ExecutorService的:

Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

关于java - 使用 ExecutorService 在具有通过循环传递的不同参数的类中同时执行方法时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56009014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com