java 8 parallel stream

34
JAVA 8 Parallel Stream 第第第 第第第第 第第第第 第第第 :、、

Upload: tengwen-wang

Post on 17-Feb-2017

197 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: JAVA 8 Parallel Stream

JAVA 8 Parallel Stream第五組:蔡詠捷、王登文、游憶文

Page 2: JAVA 8 Parallel Stream

outline• Java 8 Lambda Expression• Stream & Parallel Stream• Code Demo• Java Fork and Join using ForkJoinPool(Java7) • Parallel Stream is good or not good ?• Conclusion

Page 3: JAVA 8 Parallel Stream

Java 8 Lambda Expression

Page 4: JAVA 8 Parallel Stream
Page 5: JAVA 8 Parallel Stream

Lambda Expression• Java 8 support Lambda Expression then officially support functional

programming paradigm

• Lambda comes from the Lambda Calculus and refers to anonymous functions in programming

• Lambda Expression • Method argument  必須為一個 Functional Interface • Functional Interface : Single Abstract Method interface ( SAM

interface )• Ex : java.lang.Runnable, java.util.Comparator

Page 6: JAVA 8 Parallel Stream
Page 7: JAVA 8 Parallel Stream

Lambda Expression• Functional Interface 定義:只有一個 public abstract method 的 Java

Interface• 取代 Functional Interface 所產出的匿名類別,簡化程式碼

Page 8: JAVA 8 Parallel Stream

Lambda Expression• @FunctionalInterface Annotation

Page 9: JAVA 8 Parallel Stream

Stream• Stream 是一個 Collection Wrapper ,可以讓把集合內的元素 ( Elements

) 以串流 ( Stream ) 形式交到 Stream Methods 以 Lambda Expression 執行

• Stream 執行時期如同 Iterator,單向而不可往返,資料只讀取一次,讀取過一次後即用盡了

Page 10: JAVA 8 Parallel Stream

Stream • Lazy evalutation

• JVM 在沒有遇到 terminal operations 前,並不會執行任何 Stream method

• Short-circuit execution• JVM 會自行判斷 short-circuit ,提早完成集合元素遍歷 ( Element traversal )

• Automatic parallelism • JVM 可以把 Java Lambda Streams 同時交給多核心處理器 ( Multi-Core ) 或多線程 (Multi-Thread) 來執行,無須去編寫任何多線程代碼

Page 11: JAVA 8 Parallel Stream

Stream• Lazy evaluation

• Code example

Page 12: JAVA 8 Parallel Stream

Stream• Short circuit example

• ( fn1() || fn2() ) • if fn1() is true , fn2() will not execute

• ( fn1() && fn2() )• if fn1() is false , fn2() will not execute

Page 13: JAVA 8 Parallel Stream

Stream• Collection support

• Array support

Page 14: JAVA 8 Parallel Stream

Parallel Stream• 直接將 Stream() 改為 ParallelStream()

• 要視硬體及 JVM 配置與 JVM 負載而定• 也有可能造成負面作用 (Side-effect)

Page 15: JAVA 8 Parallel Stream

Code Demo

Page 16: JAVA 8 Parallel Stream

Example Background

https://www.dropbox.com/s/dyx9rpxj53ofyxb/%E8%9E%A2%E5%B9%95%E6%88%AA%E5%9C%96%202015-11-01%2000.19.44.png?dl=0

Page 17: JAVA 8 Parallel Stream

Let’s look some examples~

In Java7, We do that......

https://www.dropbox.com/s/dyx9rpxj53ofyxb/%E8%9E%A2%E5%B9%95%E6%88%AA%E5%9C%96%202015-11-01%2000.19.44.png?dl=0

Page 18: JAVA 8 Parallel Stream

In Java8, We just need!

Page 19: JAVA 8 Parallel Stream

We can easily change to Parallel

Page 20: JAVA 8 Parallel Stream

Java Fork and Join using ForkJoinPool

Page 21: JAVA 8 Parallel Stream

Fork

• each subtask can be executed in parallel by different CPUs, or different threads on the same CPU.

• The limit for when it makes sense to fork a task into subtasks is also called a threshold. It is up to each task to decide on a sensible threshold. It depends very much on the kind of work being done.

Page 22: JAVA 8 Parallel Stream

Join

Once the subtasks have finished executing, the task may join (merge) all the results into one result.

Page 23: JAVA 8 Parallel Stream

ForkJoinPool

A special thread pool which is designed to work well with fork-and-join task splitting.

Example:As a parameter to the ForkJoinPool constructor you pass the indicated level of parallelism you desire.ForkJoinPool forkJoinPool = new ForkJoinPool(8);

As a parameter to the ForkJoinPool constructor you pass the indicated level of parallelism you desire.

Page 24: JAVA 8 Parallel Stream

ForkJoinPool

You can submit two types of tasks. RecursiveAction and RecursiveTask.

Action won’t return any result, just compute but task will return something.

Page 25: JAVA 8 Parallel Stream

ForkJoinPool

You can submit two types of tasks. RecursiveAction and RecursiveTask.

Action won’t return any result, just compute but task will return something.

Page 26: JAVA 8 Parallel Stream

Use case:

MyRecursiveAction myRecursiveAction = new MyRecursiveAction(24); forkJoinPool.invoke(myRecursiveAction);

Advanced Topic: http://coopsoft.com/ar/CalamityArticle.html

Page 27: JAVA 8 Parallel Stream

Parallel Stream is good or not good ?Discuss Parallel Stream Good and Bad

Page 28: JAVA 8 Parallel Stream

Parallel Stream Problem• Java 8 parallel streams may

be faster,not faster or slower• streams 方法以低成本達到平行處理 (parallel processing) 的方便性, but they do almost in a

‘Black Box’• parallel processing !=

concurrent processing. But most in Java 8 「 automatic parallelization 」 example like concurrent processing.

Page 29: JAVA 8 Parallel Stream

Concurrent Processing VS Parallel Processing

Page 30: JAVA 8 Parallel Stream

Streams Good for• They allow functional

programming style using bindings.

• They allow for better performance by removing iteration. Iteration occurs with evaluation. With streams, we can bind dozens of functions without iterating.

• Streams may be infinite. Functions may be bound to infinite streams without problem. =>a short circuiting operation

Page 31: JAVA 8 Parallel Stream

Side Effect• All streams use the same

ForkJoinPool=>very long running task=> many long running sub-tasks => use each thread in pool

• one parallel subtask performing I/O has side effects on others

• Two Parallel Streams In One ForjoinPool=>Occupied Threads use

Page 32: JAVA 8 Parallel Stream

Use For Recommand• Applicable

• Multi CPU• Unrelation in I/O• Split Pool & Num Setting• Collection,Array…

• Not Applicable• One CPU =>not good in performance• In I/O Work

• Suggest :• Don’t use in default ForkJoinPool(If WorkingTask and Server Setting

tolerate…)

Page 33: JAVA 8 Parallel Stream

Conclusion• Parallel Stream seem to convenience and many benefits , but how

many question behind that is not known ?• Parallel streams is Unpredictable , and hard to be run in Correct• All most of Parallel Stream Using can’t be predictable to know the

performance of the Normal and Unrelated process 。• Please ‘Parallel Stream’ easy to allay it…(Or if you have a high

skill…)

Page 34: JAVA 8 Parallel Stream

Reference• https://dzone.com/articles/think-twice-using-java-8• http://zeroturnaround.com/rebellabs/java-parallel-streams-are-bad-for-your-health/• http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/• https://wefollownews.appspot.com/cittopnews201408_48/14848.html• http://openhome.cc/Gossip/Java/ParallelStream.html• http://www.slideshare.net/dgomezg/parallel-streams-en-java-8http://

www.slideshare.net/JustinSDK/java-se-8-lambda• http://magiclen.org/java-8-lambda/http://developer.51cto.com/art/201311/417451.htm• Custom Thread Pool 參考 : http://

stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-stream• ForkJoinPool submit, execute, invoke : http://

stackoverflow.com/questions/17881183/difference-between-execute-submit-and-invoke-in-a-forkjoinpool