Java如何调用并行传递分块数据的方法

发布于 2025-01-11 16:50:45 字数 1005 浏览 0 评论 0原文

我有一个 API 调用一次只能接受 5 个子请求。因此,如果传入的子请求超过 10 个,我需要进行分块,然后根据 SLA 并行发送它们。

下面是一个非并行分块代码,我希望并行化此调用 invokeService(bulkRequestChunk)

 int BULK_SUBREQUEST_SIZE = 5;
 // infoRequestList size = 7
 for (int i = 0; i < numOfSubRequests; i += BULK_SUBREQUEST_SIZE) {
                List<InfoRequest> infoRequestChunk;
                if (i + BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
                    infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
                } else {
                    infoRequestChunk = infoRequestList.subList(i, i + BULK_SUBREQUEST_SIZE);
                }
                BulkRequest bulkRequestChunk = new BulkRequest();
                bulkRequestChunk.setRequests(infoRequestChunk);
                BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);

                // output list to capture all chunked requests
                bulkResponseList.add(bulkResponseChunk); 
            }

I have an API call that can only accept 5 subrequests at a time. So I need to chunkify if more than 10 subrequests come in and then send them in parallel as per the SLA.

Below is a nonparallel chunkified code and I am looking to parallelize this call invokeService(bulkRequestChunk)

 int BULK_SUBREQUEST_SIZE = 5;
 // infoRequestList size = 7
 for (int i = 0; i < numOfSubRequests; i += BULK_SUBREQUEST_SIZE) {
                List<InfoRequest> infoRequestChunk;
                if (i + BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
                    infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
                } else {
                    infoRequestChunk = infoRequestList.subList(i, i + BULK_SUBREQUEST_SIZE);
                }
                BulkRequest bulkRequestChunk = new BulkRequest();
                bulkRequestChunk.setRequests(infoRequestChunk);
                BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);

                // output list to capture all chunked requests
                bulkResponseList.add(bulkResponseChunk); 
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2025-01-18 16:50:45

使用 ExecutorService 并提交任务以并行运行:

ExecutorService execServ = Executors.newFixedThreadPool(numThreads);
final List<BulkResponse> bulkResponseList = new ArrayList<>();
int BULK_SUBREQUEST_SIZE = 5;

for (int i = 0; i < numOfSubRequests; i += BULK_SUBREQUEST_SIZE) {
    List<InfoRequest> infoRequestChunk;
    if (i + BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
        infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
    } else {
        infoRequestChunk = infoRequestList.subList(i, i + BULK_SUBREQUEST_SIZE);
    }
    BulkRequest bulkRequestChunk = new BulkRequest();
    bulkRequestChunk.setRequests(infoRequestChunk);
    execServ.submit(() -> {
        BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);
        bulkResponseList.add(bulkResponseChunk);
    });
}
execServ.shutdown();
try {
    execServ.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
    
}

Use an ExecutorService and submit it tasks to run in parallel:

ExecutorService execServ = Executors.newFixedThreadPool(numThreads);
final List<BulkResponse> bulkResponseList = new ArrayList<>();
int BULK_SUBREQUEST_SIZE = 5;

for (int i = 0; i < numOfSubRequests; i += BULK_SUBREQUEST_SIZE) {
    List<InfoRequest> infoRequestChunk;
    if (i + BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
        infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
    } else {
        infoRequestChunk = infoRequestList.subList(i, i + BULK_SUBREQUEST_SIZE);
    }
    BulkRequest bulkRequestChunk = new BulkRequest();
    bulkRequestChunk.setRequests(infoRequestChunk);
    execServ.submit(() -> {
        BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);
        bulkResponseList.add(bulkResponseChunk);
    });
}
execServ.shutdown();
try {
    execServ.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文