3 Java Concurrent Patterns Advanced m3 Slides
3 Java Concurrent Patterns Advanced m3 Slides
José Paumard
PHD, JAVA CHAMPION, JAVA ROCK STAR
@JosePaumard https://github.com/JosePaumard
Agenda Two more concurrent primitives
The barrier: to have several tasks wait for each
other
The latch: to count down operations and let a
task start
Barriers
We need a given computation to be shared
among several threads
Stating the Each thread is given a subtask
Problem
When all the threads are done, then a merging
operation is run
Finding Prime Numbers
Finding Prime Numbers
Finding Prime Numbers
Only 1 thead
is working
Meaning only
1 core
is used…
Finding Prime Numbers
With 4 cores
we could go faster!
Finding Prime Numbers
Core #1
Core #2
Core #3
Core #4
Finding Prime Numbers
CPU
Core #1
T
CPU
Core #2
T
CPU
Core #3
T
CPU
Core #4
T
Finding Prime Numbers
CPU
Core #1
T
CPU
Core #2
T
Final Set
CPU
Core #3
T
CPU
Core #4
T
We need a way to distribute the computation on
several threads
We need to know when all the threads have
finished their task
We need to launch a post-processing at that
moment
Callable<List<Integer>> task = () -> findPrimes(inputSet);
This callable has to wait for the other tasks launched in parallel, when its task is
done
For that, we create a CyclicBarrier object
The parameter is the number of tasks that will be launched
Callable<List<Integer>> task = () -> {
Set<Integer> result = findPrimes(inputSet);
try {
barrier.await(); // Blocks until everybody is ready
} catch (Exception e) {...}
return result;
}
Callable<List<Integer>> task = () -> {
Set<Integer> result = findPrimes(inputSet);
try {
barrier.await(); // Blocks until everybody is ready
} catch (Exception e) {...}
return result;
}
Callable<List<Integer>> task = () -> {
Set<Integer> result = findPrimes(inputSet);
try {
barrier.await(); // Blocks until everybody is ready
} catch (Exception e) {...}
return result;
}
Callable<List<Integer>> task = () -> {
Set<Integer> result = findPrimes(inputSet);
try {
barrier.await(); // Blocks until everybody is ready
} catch (Exception e) {...}
return result;
}
Callable<List<Integer>> task = () -> {
Set<Integer> result = findPrimes(inputSet);
try {
barrier.await(); // Blocks until everybody is ready
} catch (Exception e) {...}
return result;
}
Main Executor
barrier
executor.submit()
How Does the CyclicBarrier Work?
Main Executor
barrier
Callback task
public class Worker implements Callable<List<Integer>> {
Main Executor
latch
executor.submit()
How Does the CountDownLatch Work?
Main Executor
latch
public class ServiceWorker implements Callable<List<Integer>> {
latch.countDown();
}
}
public class ServiceWorker implements Callable<List<Integer>> {
latch.countDown();
}
}
public class ServiceWorker implements Callable<List<Integer>> {
latch.countDown();
}
}
public class ServiceWorker implements Callable<List<Integer>> {
latch.countDown();
}
}
CountDownLatch latch = new CountDownLatch(3);
ExecutorService executor = Executors.newFixedThreadPool(4);
try {
latch.await(10, TimeUnit.SECONDS); // blocks until the count reaches 0
server.start();
} catch(InterruptedException e) {
// Error handling
}
CountDownLatch latch = new CountDownLatch(3);
ExecutorService executor = Executors.newFixedThreadPool(4);
try {
latch.await(10, TimeUnit.SECONDS); // blocks until the count reaches 0
server.start();
} catch(InterruptedException e) {
// Error handling
}
CountDownLatch latch = new CountDownLatch(3);
ExecutorService executor = Executors.newFixedThreadPool(4);
try {
latch.await(10, TimeUnit.SECONDS); // blocks until the count reaches 0
server.start();
} catch(InterruptedException e) {
// Error handling
}
CountDownLatch latch = new CountDownLatch(3);
ExecutorService executor = Executors.newFixedThreadPool(4);
try {
latch.await(10, TimeUnit.SECONDS); // blocks until the count reaches 0
server.start();
} catch(InterruptedException e) {
// Error handling
}
CountDownLatch
A tool to check that different threads did their task
And synchronize the beginning of subsequent tasks on the
last one to complete
Once open CountDownLatch cannot be closed again
Demo
Let us see some code!
Let us see a barrier in action
Demo What did we see?
Wrapup How to create barriers with callbacks to have
threads wait for each other
How to set a time out on the
CyclicBarrier.await() call
How to set a time out and a cancel on a
Future.get() call
Module What did we learn?
Wrapup There are two tools to trigger an action on the
completion of other actions
The CyclicBarrier: useful for parallel
computations
The CountDownLatch: useful for starting an
application on the completion of different
initializations