有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java i无法通过executor的submit方法处理列表的所有记录

在这段代码中,我创建了FixedThreadPool=3,在for循环中,我的列表包含了7条要处理的记录。对于要处理的每个记录,我将其值分配给 msnsfExecutorThread对象,该对象在顶部自动连接。在所有的赋值之后,我将msnsfExecutorThread对象传递给submit方法,该方法需要一个可运行的实现。现在,在我的例子中,问题是每当我到达msnsfexecutorhread对象的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);
        }

共 (1) 个答案

  1. # 1 楼答案

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