java 8 parallel stream

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

Post on 17-Feb-2017

176 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

JAVA 8 Parallel Stream

outlineJava 8 Lambda ExpressionStream & Parallel StreamCode DemoJava Fork and Join using ForkJoinPool(Java7) Parallel Stream is good or not goodConclusion

Java 8 Lambda Expression

Lambda ExpressionJava 8 support Lambda Expression then officially support functional programming paradigm

Lambda comes from theLambda Calculusand refers to anonymous functions in programming

Lambda Expression Method argument Functional Interface Functional Interface : SingleAbstractMethod interfaceSAM interfaceEx : java.lang.Runnable, java.util.Comparator

Lambda ExpressionFunctional Interface public abstract method Java InterfaceFunctional Interface

Lambda [email protected] Annotation

StreamStream Collection Wrapper ( Elements ) ( Stream ) Stream Methods Lambda Expression Stream Iterator

Stream Lazy evalutationJVM terminal operations Stream methodShort-circuit executionJVM short-circuit ( Element traversal )Automatic parallelism JVM Java Lambda Streams ( Multi-Core ) (Multi-Thread)

StreamLazy evaluationCode example

StreamShort circuit example( fn1() || fn2() ) if fn1() is true , fn2() will not execute( fn1() && fn2() )if fn1() is false , fn2() will not execute

StreamCollection support

Array support

Parallel Stream Stream() ParallelStream()JVMJVM(Side-effect)

Code Demo

Example Backgroundhttps://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

Lets 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

In Java8, We just need!

We can easily change to Parallel

Java Fork and Join using ForkJoinPool

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.

Join

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

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.

ForkJoinPool

You can submit two types of tasks. RecursiveAction and RecursiveTask. Action wont return any result, just compute but task will return something.

ForkJoinPool

You can submit two types of tasks. RecursiveAction and RecursiveTask. Action wont return any result, just compute but task will return something.

Use case:

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

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

Parallel Stream is good or not goodDiscuss Parallel Stream Good and Bad

Parallel Stream ProblemJava8 parallel streams may be faster,not faster or slowerstreams (parallel processing)but they do almost in a Black Boxparallel processing != concurrent processing. But most in Java 8automatic parallelizationexample like concurrent processing.

Concurrent Processing VS Parallel Processing

Streams Good forThey 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 functionswithout iterating.Streams may be infinite. Functions may be bound to infinite streams without problem. =>a short circuiting operation

Side EffectAll streams use the same ForkJoinPool=>very long running task=> many long running sub-tasks => use each thread in poolone parallel subtask performing I/O has side effects on othersTwo Parallel Streams In One ForjoinPool=>Occupied Threads use

Use For Recommand ApplicableMulti CPUUnrelation in I/OSplit Pool & Num SettingCollection,Array Not ApplicableOne CPU =>not good in performanceIn I/O WorkSuggestDont use in default ForkJoinPool(If WorkingTask and Server Setting tolerate)

ConclusionParallel Stream seem to convenience and many benefitsbut how many question behind that is not knownParallel streams is Unpredictable and hard to be run in CorrectAll most of Parallel Stream Using cant be predictable to know the performance of the Normal and Unrelated processPlease Parallel Stream easy to allay it(Or if you have a high skill)

Referencehttps://dzone.com/articles/think-twice-using-java-8http://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.htmlhttp://openhome.cc/Gossip/Java/ParallelStream.htmlhttp://www.slideshare.net/dgomezg/parallel-streams-en-java-8http://www.slideshare.net/JustinSDK/java-se-8-lambdahttp://magiclen.org/java-8-lambda/http://developer.51cto.com/art/201311/417451.htmCustom Thread Pool : http://stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-streamForkJoinPool submit, execute, invoke : http://stackoverflow.com/questions/17881183/difference-between-execute-submit-and-invoke-in-a-forkjoinpool