Java中顺序流的并行化
我需要建立一种并行管道。管道包括3个步骤Java中顺序流的并行化,java,concurrency,java-stream,Java,Concurrency,Java Stream,我需要建立一种并行管道。管道包括3个步骤 readLines():从文件中读取100.000行 firstQuery():使用readLines()中的100.000行构建查询并将其发送到DB secondQuery():使用firstQuery()结果生成并发送另一个查询 readLines()耗时1秒,返回Stream firstQuery()需要10秒才能返回Stream secondQuery()需要2分钟Stream 无论我如何处理执行器,所有方法都是按顺序执行的 我想这是因为re
readLines()
:从文件中读取100.000行firstQuery()
:使用readLines()中的100.000行构建查询并将其发送到DBsecondQuery()
:使用firstQuery()
结果生成并发送另一个查询
耗时1秒,返回readLines()
Stream
需要10秒才能返回firstQuery()
Stream
需要2分钟secondQuery()
Stream
readLines()
返回Stream
,但它不能并行,因为从文件读取是顺序的
这样做的正确方法是什么:
readLines() // limit concurrency to 2 (not more than 00.000 or lines in memory at the same time)
// as soon as lines are read from file, pass them to async
1: List<Line> -> firstQuery() -> secondQuery()
2: List<Line> -> firstQuery() -> secondQuery()
3: List<Line> -> firstQuery() -> secondQuery()
4: List<Line> -> firstQuery() -> secondQuery()
... more here
10: List<Line> -> firstQuery() -> secondQuery()
readLines()//将并发限制为2(同时不超过00.000或内存中的行)
//一旦从文件中读取了行,就将它们传递给async
1:List->firstQuery()->secondQuery()
2:列表->第一查询()->第二查询()
3:列表->第一个查询()->第二个查询()
4:List->firstQuery()->secondQuery()
... 更多
10:List->firstQuery()->secondQuery()
它看起来怎么样?
我试过的
- 将并行添加到返回流的
readLines()
- 不走运
- 将具有未来的并行自定义执行器添加到
,没有顺序firstQuery()->secondQuery()
列表
,并在该列表
上执行流操作。流
的类型看起来很有趣。您确定要的是流
,而不是流
?是的,我分批从文件中读取行,以便分批执行DB查询。我们如何知道您的readLines()
方法是如何构造流的?