5. 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
12. Stream
• Short circuit example
• ( fn1() || fn2() )
• if fn1() is true , fn2() will not execute
• ( fn1() && fn2() )
• if fn1() is false , fn2() will not execute
17. 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
21. 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.
22. Join
Once the subtasks have finished
executing, the task may join (merge) all
the results into one result.
23. 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.
24. ForkJoinPool
You can submit two types of tasks. RecursiveAction and
RecursiveTask.
Action won’t return any result, just compute but task will return
something.
25. ForkJoinPool
You can submit two types of tasks. RecursiveAction and
RecursiveTask.
Action won’t return any result, just compute but task will return
something.
30. 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
31. And Not Good For
• 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
• Not many threads running at
the same time, and in
particular no other parallel
stream