我无法通过执行人的提交方法处理列表的所有记录



在这段代码中,我正在创建FixedThreadPool=3,在for循环中,我的列表中有7条要处理的记录。对于要处理的每个记录,我将其值分配给msnsfExecutorThread对象,该对象在顶部自动连线。在所有的赋值之后,我将msnsfExecutitorThread对象传递给submit方法,该方法需要一个可运行的实现。现在,我的问题是,每当我到达msnsfExecutitorThread对象的run方法时,它总是拥有列表中最后一条记录的详细信息。它不适用于列表中的所有7条记录。它如何适用于列表中的所有记录?

ExecutorService executor = Executors.newFixedThreadPool(3);
for (TempMSISDNCollectFee tempMSISDNCollectFee : list) {
msnsfExecutorThread.setGcsAccountId(tempMSISDNCollectFee.getGcsAccountId());
msnsfExecutorThread.setMsisdn(tempMSISDNCollectFee.getMsisdn());
msnsfExecutorThread.setProcessStatus(tempMSISDNCollectFee.getProcessingStatus());
msnsfExecutorThread.setPartnerCode(tempMSISDNCollectFee.getPartnerCode());
executor.submit(msnsfExecutorThread);
}

您创建的msnsfExecutitorThread对象可能不超过1个,并继续覆盖您设置的数据。使用并发数据结构(如concurrentlinkedqueue(,将此队列设置为您创建的每个msnsfExecutorThread对象。然后在run((方法中从队列和进程轮询。

最新更新