差異處
這裏顯示兩個版本的差異處。
java:effective_java:lambdas_and_streams:use_caution_when_making_streams_parallel [2019/07/20 22:58] tony [Introduction & My Opinion] |
java:effective_java:lambdas_and_streams:use_caution_when_making_streams_parallel [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>java effective_java}} | ||
- | ====== Effective Java - Use caution when making streams parallel(working) ====== | ||
- | ===== Introduction & My Opinion ===== | ||
- | 在Java 8中,你只要用以下寫法,很容易就可以讓你的工作並行化: | ||
- | <code java> | ||
- | list.stream().parallel() .. | ||
- | list.parallelStream() .. | ||
- | </code> | ||
- | 但這個item要強調的是: 千萬別亂用!! \\ | ||
- | \\ | ||
- | 首先提到不適用的情況是使用Stream.iterate所產生的stream或者是透過limit處理的steam。這是由於預設的並行化流程並無法精準的知道該怎麼正確處理limit的邏輯。\\ | ||
- | \\ | ||
- | 能獲得較佳效能的結構有ArrayList、HashMap、HashSet、ConcurrentHashMap、arrays、int ranges與long ranges。這是由於它們的結構能夠容易被切割。\\ | ||
- | \\ | ||
- | 能獲得較佳效能的terminal operations包含reduce、min、max、count與sum;short-circuiting operations包含anyMatch、allMatch與noneMatch。(short-circuiting operations代表著滿足部分條件就可以停止繼續做下去,中文為短路操作,詳細範例可以參考[[https://www.logicbig.com/tutorials/core-java-tutorial/java-util-stream/short-circuiting.html|link]])\\ | ||
- | \\ | ||
- | collect則不適合用在parallel的情況中,因為組合集合開銷非常大。\\ | ||
- | \\ | ||
- | 這個item要強調的是:「你要有理由或者測試去證明使用parallel會加快速度,否則代價就是性能災難或是功能直接出問題。」 | ||
- | ===== Note ===== | ||
- | Effective Java第三版Item 48。 | ||
- | ===== Reference ===== | ||
- | * Effective Java, 3/e | ||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |