【发布时间】:2014-08-14 17:45:59
【问题描述】:
我可能在这里偏离了路线,但在我执行实施之前,我想确保我正确理解我在这里所做的是场景:
A 类负责收集文档信息(ID、内容等)
public class A{
private List<Documents> docs = new ArrayList<Documents>
public InputStream retrieveAllDocuments(){
// Loop here to retrive the 10000 document chunk.
while(docs.size() < 10000000){
List<TrimmedDownDoc> tempList = callSlowApiToRetrieveDoc();
ExecutorService.call(InnerA);
}
return an InputStream;
}
private class InnerA implements Callable<Boolean> {
private List<TrimmedDownDoc> tempList
public InnerA(List<...> tempList){
this.tempList = tempList;
}
public Boolean call() {
// extract temp list and populate docs list in a threadsafe manner.
// Once this is done I would like to push them to the stream
}
}
}
我正在寻找最好的方法,以便 A 类的使用者可以在流文档可用时读取它们。这将阻止 A 类构建一个文档列表,该列表将是数百万个长期影响内存使用的文档。目前,除了保存到临时文件之外,我并没有真正看到其他选择,但如果可能的话,我想通过使用正确的 Java 对象/流来避免这种情况。
谢谢,
【问题讨论】:
-
您不需要文件来创建输入流。但目前你的问题是模糊地说什么。文档如何变成
InputStream或者文档列表如何变成单个InputStream?为什么?InputStream很可能会被读取并转换为您可以直接从文档对象中创建的东西,那么为什么要绕道呢?
标签: java multithreading stream inputstream