Parallel Asynchronous Programming
Parallel Asynchronous Programming
&
Asynchronous Programming
In
Modern Java
Dilip Sundarraj
About Me
• Dilip
• Techniques to write Fast Performing Code using Functional Style Concurrency APIs in
Java
• Developers who has the need to write code that executes faster
• Developers who has the need to write code that executes in Parallel
Lamdas
Data Parallelism Streams API
https://en.wikipedia.org/wiki/Java_version_history
Concurrency
vs
Parallelism
Concurrency
• Concurrency is a concept where two or more task can run simultaneously
• In Java, Concurrency is achieved using Threads
• Are the tasks running in interleaved fashion?
• Are the tasks running simultaneously ?
• Race Condition }
}
//Starting the thread
2 helloThread.start();
• Synchronized Statements/Methods worldThread.start();
Task
• Parallelism is a concept in which tasks are
literally going to run in parallel
1 fork
SubTask1 SubTask2
• Parallelism involves these steps: fork fork
join
Parallelism Example Fork/Join (Parallelism)
fo
k r
fo
rk
fo
rk
Bob Jamie Jill Rick
Pr Pr Pr
Bob Jamie Jill Rick oc oc oc
Pr
es es es
o
sS sS sS
ce
ss
eq eq eq
uppercase uppercase uppercase uppercase ue ue ue
Se
2 nt nt nt
qu
ia JAMI ia RICK ia
en
BOB lly lly JILL lly
tia
E
lly
Core1 Core2 Core3 Core4
jo
i
BOB JAMIE JILL RICK 3
jo
n
i n
BOB, JAMIE, JILL, RICK
jo
n i
Parallelism Example
}
}
Concurrency vs Parallelism
ProductInfo
size, color, price
Service
ProductId
Product Review
No of reviews,
Service Overall rating
Product
Threads
Threads API
• Threads API got introduced in Java1
• Runnable, Thread
• Low level
ExecutorService
WorkQueue
ThreadPool
T T
Q Q
T1 T2
CompletionQueue T T
Q Q
T3 T4
Working Of ExecutorService
ExecutorService
WorkQueue ThreadPool
T T
Client Task
Q Q
T1 T2
Future
T T
CompletionQueue
Q Q
T3 T4
Resul
t
Limitations of ExecutorService
• Designed to Block the Thread
fo
k r
fo fo
size and execute those tasks in parallel rk rk
Bob Jamie Jill Rick
Pr
Pr
Pr
Pr
o
oc
oc
o
ce
ce
es
es
ss
ss
sS
sS
Se
approach
Se
2
eq
eq
qu
qu
ue
ue
JAMI RICK
en
en
BOB JILL
nt
nt
tia
tia
ia
ia
E
lly
lly
lly
lly
[BOB, [JILL, RICK]
JAMIE]
jo
jo
in
i
3
n
Watch "Concurrency vs Parallelism”
BOB, JAMIE, JILL, RICK
jo
n i
How does Fork/Join Framework Works ?
ForkJoin Pool D
ou
bl
eE
nd
ed
W
or
k
Q
Tas Tas Tas Tasueu T1
k k k k e(
d ec
k)
Task
WorkStealing Tas
Client ForkJoinTask k T2
W
or
ke
Result rT
Shared Work Queue hr
ea
ds
Tas T3
k
Tas T4
k
ForkJoin Task
Fork/Join (Parallelism)
• ForkJoin Task represents part of the data and Bob, Jamie, Jill, Rick
its computation
fo
k r
• ForkJoinTask fo
rk
fo
rk
Bob Jamie Jill Rick
• RecursiveTask -> Task that returns a Fork/Join
value Task
Pr
Pr
Pr
Pr
o
oc
oc
o
ce
ce
es
es
ss
ss
sS
sS
Se
Se
eq
eq
qu
qu
ue
ue
JAMI
• RecursiveAction -> Task that does not RICK
en
en
BOB JILL
nt
nt
tia
tia
ia
ia
E
lly
lly
lly
lly
return a value
jo
in
in
BOB, JAMIE, JILL, RICK
jo
n i
Fork/Join Example
ForkJoin - UseCase
Input Output
Intermediate
1
3
] Pipeline
Terminal
ParallelStreams
Stream
Parallel Stream
Parallel Streams - UseCase
Input Output
• Unit Testing allows the developer or the app team to make enhancements to the existing
Sequential
parallel()
Parallel
When to use sequential() and parallel() ?
Used these functions when I would like to evaluate between sequential() and parallel()
Overview of the
Retail
Checkout
Service
Checkout Service(BackEnd)
UP
• Execute
• Data chunks are applied to the Stream Pipeline and the Intermediate operations executed in a Common ForkJoin Pool
• Combine
• collect(toList())
parallelStream() - How it works ?
parallelStreams()
cartItemsList CheckoutService
return cartItem;
Execute 2 })
Process Sequentially Process Sequentially Process Sequentially Process Sequentially
Common ForkJoinPool
.filter(CartItem::isExpired)
cartItem.collect(toList()); cartItem cartItem cartItem
cartItemsList combine
Comparing
ArrayList vs LinkedList
ParallelStreams
Performance
Spliterator in ParallelStreams
[ 1, 2, 3, 4 ] -> [2, 4, 6, 8]
Value * 2
Summary - Spliterator in ParallelStreams
90
90
90
Parallel Streams - Final Computation Result Order
• Type of Collection
• Example : ArrayList
• Example : Set
• Used as a terminal operation in Streams API • Used as a terminal operation in Streams API
• Feature rich and used for many different use • Reduce the computation into a single value
cases
• Sum, Multiplication
• Example
• Example
• collect(toList()), collect(toSet())
• Sum -> reduce(0.0, (x , y)->x+y)
• collect(summingDouble(Double::doubleValue
• Multiply -> reduce(1.0, (x , y)->x * y)
));
How reduce() works ?
[ 1 ,2 ,3 ,4 ]
0+1 => 1
3+3 => 6
6+4 => 10
The reduce( ) function performs an immutable computation throughout in each and every step.
How reduce() with ParallelStream works ?
Sum of N numbers
1
[ 1 ,2 ,3 ,4 ] [ 5 ,6 ,7 ,8 ]
Split Split Split Split
36
0 0 0 0
[ 1 ,2 ] [ 3 ,4 ] [ 5 ,6 ] [ 7 ,8 ]
1 Spliterator
3 7 11 15
reduce()
reduce()
2 ForkJoinPool
3 10 26
reduce() reduce()
3 Reduce
36
Want to learn more ?
Collect() & Reduce()
Hands-On
Identity in reduce()
Identity in reduce()
• Addition: Identity = 0
• 0 + 1 => 1
• 0 + 20 => 20
• Multiplication : Identity = 1
• 1 * 1 => 1
cartItemsList CheckoutService
Split
Split 1 cartItemsSplit cartItemsSplit
Split Split
cartItemsList combine
Common ForkJoin Pool
parallelStreams() T2
W
or
ke
Result rT
Shared Work Queue hr
ea
ds
90
T3
• ParallelStreams
• CompletableFuture
• parallelStreams()
• Yes
Modifying
Default parallelism
in
Parallel Streams
Modifying Default parallelism
R
O
00
=1
ism
el
; ll
"a)ra
0.0p
"o1n
",m
mm
licso
lel.
aol o
arP
.opin
oknJ
mor
otm.F
lr.ecn
ouor
nnPc
ocio
kiJl.
ourt
.Fa.
natv
rej
ur-D
nc
.c o
til
.u
va
"ja
y(
Parallel Streams - Summary
Parallel Streams - When to use them ?
Parallel Streams - When to use them ?
• Parallel Streams
• Split
• Execute
• Combine
Parallel Streams - When to use them ?
• Lots of data
• Parallel Streams
• Split
• Execute
• Combine
• Introduced in Java 8
• Responsive:
1
• Fundamentally Asynchronous
• Call returns immediately and the response will be sent when
its available
• Resilient:
• Exception or error won’t crash the app or code
• Elastic:
3 2
• Asynchronous Computations normally run in a pool of threads
• No of threads can go up or down based on the need
• Message Driven: 4
• Factory Methods
• Exception Methods
• Returns CompletableFuture<Void>
• Returns CompletableFuture<T>
CompletableFuture
]
1
2
3 Pipeline
Unit Testing
CompletableFuture
Combing independent
Async Tasks
using
“thenCombine"
thenCombine()
• This is a Completion Stage Method
Service1
Service2
• CompletionStage , BiFunction
• Returns a CompletableFuture
thenCompose
thenCompose()
• Transform the data from one form to another public CompletableFuture<String> worldFuture(String input) {
return CompletableFuture.supplyAsync(()->{
delay(1000);
return input+" world!";
});
• Input is Function Functional Interface }
ProductInfo
Service
ProductId
Product
Review
Service
Product
Combining
Streams
&
CompletableFuture
Product Service with Inventory
ProductInfo
Service
ProductId
Product
Review
Service
Exception
Handling
In
CompletableFuture
Exception Handling in Java
• Exception Handling in Java is available since the inception of Java
]
Exception Handling in CompletableFuture
• CompletableFuture is a functional style API
Exception Handling in CompletableFuture
try/catch
Exception Handling in CompletableFuture
• handle()
Ca
tc
h
Ex
• exceptionally()
c
ep
tio
n
an
• whenComplete()
d
Re
Catch Exception and Does not Recover
co
v
er
Exception Handling using handle()
Success Path
handle handle
Failure Path
handle handle
Exception Handling using exceptionally()
Success Path
Failure Path
exceptionally exceptionally
whenHandle()
whenHandle()
• Catches the Exception but does not recover from the exception
Exception Handling using whenComplete()
Success Path
whenComplete whenComplete
Failure Path
whenComplete whenComplete
Product Service with Inventory
!
ProductInfo
Service
ProductId
Product
Review
Service
CompletableFuture
Default ThreadPool
CompletableFuture - ThreadPool
completableFuture() T2
W
or
ke
rT
Shared Work Queue hr
Re ea
su ds
lt
T3
T4
Watch “Internals of Common ForkJoin Pool” Lecture
CompletableFuture
&
User Defined ThreadPool
using
ExecutorService
Why use a different ThreadPool ?
• ParallelStreams
• CompletableFuture
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Threads
In
CompletableFuture
Async()
Overloaded Functions
In
CompletableFuture
Async Overloaded Functions
• thenAccept()
Async() Overloaded Functions
Regular Functions Async() overloaded Functions
• thenCombine() • thenCombineAsync()
• thenApply() • thenApplyAsync()
• thenCompose() • thenComposeAsync()
• thenAccept() • thenAcceptAsync()
Async() Overloaded Functions
• Use this when you have blocking operations in your Completablefuture pipeline
Introduction to
Spring WebClient
and
Overview of the GitHub Jobs API
About this section
• Spring WebClient is a rest client library that’s got released as part of Spring 5
Service 1
Client Service 2
Service 2
anyOf()
anyOf() - Dealing with Multiple CompletableFutures
• Use anyOf() when you are dealing with retrieving data from multiple Data Sources
D
B
SOAP Service
TimeOuts
In
CompletableFuture
Timeouts in CompletableFuture
• orTimeout() in CompletableFutureAPI