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

标签 java multithreading parallel-processing executorservice

我正在尝试编写一个程序来处理大约一百万名大量员工的操作。我正在使用 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/

相关文章:

iphone - 主线程在等待 NSOperationQueue 操作取消时无限期挂起 [仅在设备上!]

python - 从外部函数访问 QLCDNumber 对象

c++ - 从接收方的角度来看,我如何确保已收到使用 MPI_Isend 发送的所有消息?

multithreading - 使用汇总的Scala并行频率计算不起作用

c# - MaxDegreeOfParallelism = 2 显示 3 个线程

java - Hibernate 不插入外键 ManyToOne 实体

android - 应用查询服务器时需要显示加载屏幕

java - 我无法创建常规项目

java - 如何在android中替换字符串中的单词

java - TestNG 报告中的自定义测试方法名称