小编Nis*_*ade的帖子

如何使用 CompletableFuture 对 ArrayList 中的一批元素执行操作?

Java版本:11

我有对象列表,我想对它们执行某些操作,其中一个操作取决于另一个操作的结果。为了以异步方式实现此工作流程,我正在使用CompletableFuture.

目前,我正在通过将列表分区为子列表并将每个子列表赋予 来实现此目的CompletableFuture,因此线程池中的每个线程都可以开始处理该子列表。

我使用和工作的上述方法的代码是:

List<SomeObject> someObjectList // original list

List<List<SomeObject>> partitionList = ListUtils.partition(someObjectList, partionSize);

partitionList.forEach(subList -> {
    CompletableFuture.supplyAsync(() ->  firstOperation(subList), executorService)
            .thenAcceptAsync(firstOpresult -> secondOperationWithFirstOpResult(firstOpresult),executorService);
});

public static List<String> firstOperation(List<SomeObject> subList){
        //perform operation
        return List<String>;
}

public static void secondOperationWithFirstOpResult(List<String> firstOpProducedList) {
        //perform operation
        //print results.
}

Run Code Online (Sandbox Code Playgroud)

这里的问题是对原始列表进行分区,

因为如果我的原始列表有 10 万条记录,分区大小为 100(这意味着我希望每个子列表中有 100 个项目),我将有 1000 个子列表对象,每个对象包含 100 个元素,考虑到内存中有这么多对象,这可能不太好,此外,如果分区大小是用户控制/配置控制器,则较小的分区大小将导致子列表对象数量过多。

而不是对原始列表进行分区,

  1. 我想要原始列表。(比如说 100 个元素)
  2. 在原始列表上有一个 startIndex 和 endIndex(比如 0 到 9、10 到 19...)
  3. 并将每个批次分配给一个线程,在带有 CompletableFuture 的线程池中
  4. 所以这个线程可以执行这两个操作。 …

java multithreading java-8 completable-future

5
推荐指数
1
解决办法
736
查看次数

通过“_id”查找文档时,Id的查询和Term Query有什么区别?

我想通过“_id”获取文档,我有3个选择:

GET document by "_id" GET order/_doc/001

Use Id's Query, GET order/_search { "query": { "ids" : { "values" : ["001"] } } } Though Id's query takes array of Id's but I will be using it to get only one document at a time, so just passing one id in "values" : ["001"]

Use Term Query GET order/_search { "query": {"term": {"_id" : "001"}}}
Run Code Online (Sandbox Code Playgroud)

我想知道 Id 的查询和术语查询之间有什么区别,性能方面以及我应该注意的任何其他点?

我应该选择哪一个(ID 和术语查询之间)?

任何帮助深表感谢:)

elasticsearch elastic-stack

1
推荐指数
1
解决办法
855
查看次数