Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

java - Alternative of parallelstream to run method in parallel

I have been using a method with paralleStream() in order to perform some operations on files like translations and modification (different file types and size). But i have noticed parallelStream sometimes behaves in a wierd way giving me different exceptions or Error outputs printing in the output file instead of proper translations.

On changing parallelStream() to sequentialStream() it works fine. But the problem is , since it is CPU-intensive operations, i need to have better performance in terms of speed.

what is the alternative of parallelstream that would work better than that in such situation.

Use case : Modification of files

Translation of file content from one language to another, so it requires to parsing through each segment in order to do the translation for each segments from source locale to target language locale.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The alternatives of parallelStream you can try are ExecutorService and Fork/Join Framework.

Υou can find here the results of testing the above three options when taking into account different parameters (e.g. number of threads).

Now what is better in each case is relevant to the case and its parameters. As you can see from the results of those tests, parallelStream achieved both the best and the worst execution times. So, it really depends on the parameters of your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...