我很难弄清楚这一点,可以从比我更有经验和知识的人士那里得到一些帮助。
基本问题是我需要获取一个对象列表,然后对于返回的每个对象,获取一些详细信息,然后将这些详细信息拼接到该对象中。我想对此有所作为;我需要首先获取DataFiles列表,但一旦有了该列表,就可以同时调用所有其Tag,然后等待所有getTags响应返回,然后再处理它们。>
public class DataFile {
// DataFileDao returns all DataFile properties, except Tags
private List<Tags> tags;
...
}
我只是无法弄清楚如何使用CompletableFutures和流来实现此功能。不过,这是我正在使用的基本代码,如果有人可以帮助我完成任务,我将不胜感激:
public CompletableFuture<List<DataFile>> getDataFilesWithTags() {
final CompletableFuture<List<DataFile>> dataFileFutures = this.dataFileDao.getDataFiles()
.thenApply(HttpResponse::body).thenApply(this::toDataFileList);
final CompletableFuture<List<List<Tag>>> tagFutures = dataFileFutures
.thenCompose(dataFiles -> HttpUtils.allAsList(dataFiles.stream()
.map(file -> this.tagDao.getLatestTagsForDataFile(file.getId())).collect(toList())));
final CompletableFuture<List<DataFile>> filesWithTags = dataFileFutures.thenCombine(tagFutures,
(files, tags) -> {
for (int i = 0; i < files.size(); i++) {
files.get(i).setTags(tags.get(i));
}
return files;
});
return fileWithTags;
}
/**
* Transforms a generic {@link List} of {@link CompletableFuture}s to a {@link CompletableFuture} containing a
* generic {@link List}.
*
* @param futures the {@code List} of {@code CompletableFuture}s to transform
* @param <T> the type of {@link CompletableFuture} to be applied to the {@link List}
* @return a {@code CompletableFuture} containing a {@code List}
* @throws NullPointerException if {@code futures} is null
*/
public static <T> CompletableFuture<List<T>> allAsList(final List<CompletableFuture<T>> futures) {
Validate.notNull(futures, "futures cannot be null");
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
.thenApply(ignored -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
}
必须要有一种更清洁,更实用的方法,对吧?
我想做的事情的抽象表示:
public class ParentObject {
// RETURNED BY ParentObjectDao.getAllParentObjects()
private String id;
// *NOT* RETURNED BY ParentObjectDao.getAllParentObjects()
// MUST BE RETRIEVED BY MAKING SUPPLEMENTAL CALL TO ParentObjectDao.getParentsChildren(String parentObjectId)
private List<ChildObject> details;
}
public class ChildObject {
private String id;
private String description;
}
public class ParentObjectDao {
public CompletableFuture<List<ParentObject>> getAllParentObjects();
public CompletableFuture<List<ChildObject>> getChildrenForParent(String parentObjectId);
}
public class Something {
private final ParentObjectDao dao;
public List<ParentObject> getParentObjectsWithChildren(){
// PSEUDO-LOGIC
final List<ParentObject> parentsWithChildren = dao.getAllParentObjects()
.thenApply(List::stream)
.thenCompose(parentObject -> dao.getChildrenForParent(parentObject.getId()))
.thenApply(parentObject::setChildren)
.collect(toList);
return parentsWithChildren;
}
}
答案 0 :(得分:2)
您拥有的代码并没有真正并行化。您一次只处理一个CompletableFuture
,并将其链接起来。因此,如果您有1000个数据文件,它们仍将按顺序处理。
另外,从设计和可读性的角度来看,CompletableFuture
的运行水平太低了(您真的需要链接thenApply(HttpResponse::body).thenApply(this::toDataFileList)
吗? CompletableFuture
仅代表一种方法?)
使用您的伪代码,怎么办呢?
CompletableFuture<List<ParentObject>> populateAsync(List<ParentObject> parents) {
//get the children of each parent in parallel, store the futures in a list
List<CompletableFuture<ParentObject>> futures =
parents.stream()
.map(parent ->
parentObjectDao.getChildrenForParent(parent.getId())
.thenApply(parent::setChildren)) //assuming setChildren returns the parent object
.collect(Collectors.toList()); //we need this stream terminal operation to start all futures before we join on the first one
//wait for all of them to finish and then put the result in a list
return CompletableFuture.supplyAsync(() ->
futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
}
您将可以执行以下操作:
CompletableFuture<List<ParentObject>> getAllParentObjects()
.thenApply(this::populateAsync)
(我可能会遇到一些语法错误,因为我只是直接在这里写的,但是您应该明白这一点。)