SlideShare a Scribd company logo
1CONFIDENTIAL
Concurrency Utilities in
Java 8
04.07.2015
2CONFIDENTIAL
Agenda
• Parallel Tasks
• Parallel Streams
• Parallel Array operations
3CONFIDENTIAL
Agenda
• CompletableFuture
• ConcurrentHashMap
• Scalable Updatable Variables
• StampedLock
4CONFIDENTIAL
Parallel Tasks
5CONFIDENTIAL
Parallel Tasks
• Parallel tasks are represented by implementations of the
ForkJoinTask that are submitted to a ForkJoinPool instance for
execution
• Provides a direct way to implement a divide-and-conquer
mechanism for executing multiple tasks in parallel (in regard to
executing many independent tasks with the ExecutorService)
6CONFIDENTIAL
Parallel Tasks
• Typical implementations of parallel tasks do not extend
directly ForkJoinTask
• RecursiveTask instances can be used to execute parallel tasks
that return a result
• RecursiveAction instances can be used to execute parallel
tasks that do not return a result
7CONFIDENTIAL
Parallel Tasks
• ForkJoinPool.commonPool() - introduced in Java 8 in order to
retrieve the common pool that is basically a fork-join pool for
all ForkJoinTasks that are not submitted to a pool
• The common pool is used to implement parallel streams and
parallel array operations
• The common pool is as easy way to retrieve a ForkJoinPool for
parrallel operations that also improves resource utilization
8CONFIDENTIAL
Parallel Tasks
public class NumberFormatAction extends RecursiveAction {
…
@Override
protected void compute() {
if (end - start <= 2) {
System.out.println(Thread.currentThread().getName() + " " + start + " " +
end);
} else {
int mid = start + (end - start) / 2;
NumberFormatAction left = new NumberFormatAction(start, mid);
NumberFormatAction right = new NumberFormatAction(mid + 1, end);
invokeAll(left, right);
}
public static void main(String[] args) {
NumberFormatAction action = new NumberFormatAction(1, 50);
ForkJoinPool.commonPool().invoke(action);
}}}
9CONFIDENTIAL
Parallel Streams
10CONFIDENTIAL
Parallel Streams
• Streams (sequential and parallel) are introduced in java 8 with
the utilities in the java.util.stream package
• You can get a parallel stream from a collection using the
parallelStream() method or convert a sequential stream to a
parallel one using the parallel() method
11CONFIDENTIAL
Parallel Streams
Stream.of(1,2,3,4,5,6,7,8,9).parallel().forEach(System.out::println);
Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} ).parallel().count();
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(100); list.add(200); list.add(300);
Stream<Integer> stream1 = list.stream().parallel();
Stream<Integer> stream2 = list.parallelStream();
int[] values = IntStream.range(1000, 2000).parallel().toArray();
Arrays.stream(new double[]{1.1, 2.2, 3.3}).parallel().sum();
12CONFIDENTIAL
Parallel Streams
• However …
– Be careful when using parallel streams - they may be slower then
sequential streams
– Always do proper benchmarks (e.g. using JMH)
13CONFIDENTIAL
Parallel Streams
• Parallel streams may not be proper when:
– A data structure that cannot be split well is used (e.g. LinkedList)
– Tasks are not independent and they cannot be split independently
– There are a lot of IO operations (file, network etc. ) or synchronization
– Simple operations are performed that may be optimized when using a
sequential stream
14CONFIDENTIAL
Parallel Streams
• Parallel streams may not be proper when:
– A data structure that cannot be split well is used (e.g. LinkedList)
– Tasks are not independent and they cannot be split independently
– There are a lot of IO operations (file, network etc. ) or synchronization
– Simple operations are performed that may be optimized when using a
sequential stream
(Some of the limitations are implied due to the shared common Fork/Join pool being used for parallel streams)
15CONFIDENTIAL
Parallel Streams
• Imagine a JavaEE application creating a parallel stream …
• … and several other applications creating parallel streams
• ALL of them use the common Fork/Join pool by default …
=> Simply do not create a parallel stream in a JavaEE application
16CONFIDENTIAL
Parallel Streams
public static void listStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.stream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
public static void listParallelStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.parallelStream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
VS
17CONFIDENTIAL
Parallel Streams
public static void listStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.stream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
public static void listParallelStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.parallelStream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
VS
List is a LinkedList instance
64 ms 309 ms
18CONFIDENTIAL
Parallel Streams
public static void listStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.stream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
public static void listParallelStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.parallelStream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
VS
List is an ArrayList instance
55 ms 280 ms
19CONFIDENTIAL
Parallel Streams
public static void listStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.stream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
public static void listParallelStream() {
List<Integer> numbers =
getRandomNumbers(10_000_000);
long start = System.nanoTime();
numbers.parallelStream().
filter(num -> num%2 == 0).count();
long end = System.nanoTime();
System.out.println((end – start)/1000);
}
VS
(second run for both methods)
6 ms 1 ms
20CONFIDENTIAL
Parallel Streams
• System.nanoTime() is the one of the most naïve (and incorrect)
approaches for doing benchmarks in practices …
• It does not take into account:
– a warm up phase before timing (triggers all initializations and JIT
compilations)
– side work done by the JVM (such as GC, output from another threads …)
– many other things depending on the type of performance statistics being
gathered …
21CONFIDENTIAL
Parallel Streams
• Possible libraries you can use to measure performance of
parallel streams include:
– JMH
– Caliper
– JUnitBenchmarks
22CONFIDENTIAL
Parallel Streams
• The limitations imposed by the Fork/Join framework used by
parallel streams leads to a rigid critique:
“The JDK1.8 engineers are using the recursively decomposing F/J
framework, with its very, very narrow measure, as the engine for
parallel programming support simply because they don’t have anything
else.”
23CONFIDENTIAL
Parallel Streams
(demo)
24CONFIDENTIAL
Parallel Array Operations
25CONFIDENTIAL
Parallel Array Operations
• JDK 8 introduces a number of new methods in the Arrays utility
that allow parallel manipulation of arrays
• The methods are divided in three categories:
– parallelSort() – sorts an array in parallel
– parallelPrefix() – performs a cumulative operations on an array in parallel
– parallelSetAll() – sets all of the elements in an array in parallel
26CONFIDENTIAL
Parallel Streams
Integer[] array = { 16, 7, 26, 14, 77 };
Arrays.parallelSort(array);
[7, 14, 16, 26, 77]
Integer[] array = { 16, 7, 26, 14, 77 };
Arrays.parallelPrefix(array, (a, b) -> a * b);
[16, 112, 2912, 40768, 3139136]
Integer[] array = new Integer[7];
Arrays.parallelSetAll(array, i -> i);
[0, 1, 2, 3, 4, 5, 6]
27CONFIDENTIAL
CompletableFuture
28CONFIDENTIAL
CompletableFuture
• Provides a facility to create a chain of dependent non-blocking tasks
- an asynchronous task can be triggered as the result of a completion
of another task
• A CompletableFuture may be completed/cancelled by a thread
prematurely
• Such a facility is already provided by Google's Guava library
Task 1 Task 2 Task 3 Task 4triggers triggers triggers
29CONFIDENTIAL
CompletableFuture
• Provides a very flexible API that allows additionally to:
o combine the result of multiple tasks in a CompletableFuture
o provide synchronous/asynchronous callbacks upon completion of a
task
o provide a CompletableFuture that executes when first task in group
completes
o provide a CompletableFuture that executes when all tasks in a group
complete
30CONFIDENTIAL
CompletableFuture
CompletableFuture<Integer> task1 = new
CompletableFuture<Integer>();
// forcing completing of future by specifying result
task1.complete(10);
31CONFIDENTIAL
CompletableFuture
CompletableFuture<Integer> task1 = CompletableFuture
.supplyAsync(() -> { … return 10;});
// executed on completion of the future
task1.thenApply((x) -> {…});
// executed in case of exception or completion of the
future
task1.handle((x, y) -> {…});
// can be completed prematurely with a result
// task1.complete(20);
System.err.println(task1.get());
32CONFIDENTIAL
CompletableFuture
CompletableFuture<Object> prev = null;
Supplier<Object> supplier = () -> { … };
for (int i = 0; i < count; i++) {
CompletableFuture<Object> task;
if (prev != null) {
task = prev.thenCompose((x) -> {
return CompletableFuture.supplyAsync(supplier);});
} else {
task = CompletableFuture.supplyAsync(supplier);
}
prev = task;
}
prev.get();
33CONFIDENTIAL
ConcurrentHashMap
34CONFIDENTIAL
ConcurrentHashMap
• ConcurrentHashMap<V, K> class completely rewritten in order
to improve its usage as a cache and several new methods have
been added as part of the new stream and lambda
expressions:
o forEach
o forEachKey
o forEachValue
o forEachEntry
35CONFIDENTIAL
Scalable Updatable Variables
36CONFIDENTIAL
Scalable Updatable Variables
• Maintaining a single variable that is updatable from many threads is
a common scalability issue
• Atomic variables already present in the JDK serve as a means to
implement updatable variables in a multithreaded environment
• New classes are introduced in order to reduce atomicity guarantees
in favor of high throughput - DoubleAccumulator, DoubleAdder,
LongAccumulator, LongAdder
37CONFIDENTIAL
DoubleAccumulator accumulator = new DoubleAccumulator((x, y) -> x + y, 0.0);
// code being executed from some threads
accumulator.accumulate(10);
accumulator.accumulate(20);
System.out.println(accumulator.doubleValue());
Scalable Updatable Variables
38CONFIDENTIAL
StampedLock
39CONFIDENTIAL
StampedLock
• A very specialized type of explicit lock
• Similar to ReentrantReadWriteLock but provides additionally
conversion between the lock modes (writing, reading and optimistic
reading)
• Optimistic read lock is a "cheap" version of a read lock that can be
invalidated by a read lock
• Lock state is determined by version and lock mode
40CONFIDENTIAL
StampedLock
• Example
StampedLock sl = new StampedLock();
long stamp = sl.writeLock();
try {
// do something that needs exclusive locking
} finally {
sl.unlockWrite(stamp);
}
41CONFIDENTIAL
StampedLock
• Example
public long getValue() {
long stamp = sl.tryOptimisticRead();
long value = this.value;
if (!sl.validate(stamp)) {
stamp = sl.readLock();
try {
value = this.value;
} finally {
sl.unlockRead(stamp);
}
}
return value;
}
42CONFIDENTIAL
Thank you
43CONFIDENTIAL
Resources
Concurrency Utility Enhancements in Java 8
https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/changes8.html
JVM concurrency: Java 8 concurrency basics
http://www.ibm.com/developerworks/library/j-jvmc2/index.html
Everything about Java 8
http://www.techempower.com/blog/2013/03/26/everything-about-java-8/
Java Specialists newsletter
http://www.javaspecialists.eu/archive/archive.jsp
44CONFIDENTIAL
Resources
Java Tutorials: Parallelism
https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html
Think twice before using Java 8 parallel streams
http://java.dzone.com/articles/think-twice-using-java-8
Parallel operations in Java 8
http://www.drdobbs.com/jvm/parallel-array-operations-in-java-8/240166287
Package java.util.stream
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
How do I write a correct microbenchmark in Java
http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java
45CONFIDENTIAL
Resources
Fork/Join Framework Tutorial: ForkJoinPool Example
http://howtodoinjava.com/2014/05/27/forkjoin-framework-tutorial-forkjoinpool-example/
Java 8 tutorial: Streams by Examples
http://howtodoinjava.com/2014/04/13/java-8-tutorial-streams-by-examples/
When to use parallel streams
http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html
A Java Fork/Join Calamity
http://www.coopsoft.com/ar/CalamityArticle.html
A Java Parallel Calamity
http://www.coopsoft.com/ar/Calamity2Article.html
46CONFIDENTIAL
Resources
Java Parallel Stream Performance
http://stackoverflow.com/questions/22999188/java-parallel-stream-performance
Java Theory and practice: Anatomy of a flawed microbenchmark
https://www.ibm.com/developerworks/java/library/j-jtp02225/
Java 8 Concurrency Tutorial: Synchronization and Locks
http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/
Ad

More Related Content

What's hot (20)

Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Hoang Nguyen
 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdf
José Paumard
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Simplilearn
 
.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6
Amin Mesbahi
 
Outgoing Call Flow ~ Android ( < Nougat).
Outgoing Call Flow ~ Android ( < Nougat).Outgoing Call Flow ~ Android ( < Nougat).
Outgoing Call Flow ~ Android ( < Nougat).
Arindom Saikia
 
Selenium
SeleniumSelenium
Selenium
Rakshitha Raviprakash
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
Dmitry Buzdin
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
Maven Basics - Explained
Maven Basics - ExplainedMaven Basics - Explained
Maven Basics - Explained
Smita Prasad
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
Visual Engineering
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
Kostas Saidis
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
CNIT 126: Ch 6: Recognizing C Constructs in AssemblyCNIT 126: Ch 6: Recognizing C Constructs in Assembly
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Sam Bowne
 
Proposal
ProposalProposal
Proposal
Constantine Priemski
 
Laravel.IO A Use-Case Architecture
Laravel.IO A Use-Case ArchitectureLaravel.IO A Use-Case Architecture
Laravel.IO A Use-Case Architecture
Shawn McCool
 
SpringOne Tour: Spring Boot 3 and Beyond
SpringOne Tour: Spring Boot 3 and BeyondSpringOne Tour: Spring Boot 3 and Beyond
SpringOne Tour: Spring Boot 3 and Beyond
VMware Tanzu
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Hoang Nguyen
 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdf
José Paumard
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Simplilearn
 
.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6
Amin Mesbahi
 
Outgoing Call Flow ~ Android ( < Nougat).
Outgoing Call Flow ~ Android ( < Nougat).Outgoing Call Flow ~ Android ( < Nougat).
Outgoing Call Flow ~ Android ( < Nougat).
Arindom Saikia
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
Maven Basics - Explained
Maven Basics - ExplainedMaven Basics - Explained
Maven Basics - Explained
Smita Prasad
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
Visual Engineering
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
Kostas Saidis
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
CNIT 126: Ch 6: Recognizing C Constructs in AssemblyCNIT 126: Ch 6: Recognizing C Constructs in Assembly
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Sam Bowne
 
Laravel.IO A Use-Case Architecture
Laravel.IO A Use-Case ArchitectureLaravel.IO A Use-Case Architecture
Laravel.IO A Use-Case Architecture
Shawn McCool
 
SpringOne Tour: Spring Boot 3 and Beyond
SpringOne Tour: Spring Boot 3 and BeyondSpringOne Tour: Spring Boot 3 and Beyond
SpringOne Tour: Spring Boot 3 and Beyond
VMware Tanzu
 

Similar to Concurrency Utilities in Java 8 (20)

Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
kshanth2101
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
Minh Tran
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
Abhijit Gaikwad
 
Java 9
Java 9Java 9
Java 9
Joanna Sokołowicz
 
Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
Ken Coenen
 
Modern_Java_Workshop manjunath np hj slave
Modern_Java_Workshop manjunath np hj slaveModern_Java_Workshop manjunath np hj slave
Modern_Java_Workshop manjunath np hj slave
gangadharnp111
 
Java 8 concurrency abstractions
Java 8 concurrency abstractionsJava 8 concurrency abstractions
Java 8 concurrency abstractions
Nawazish Mohammad Khan
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
Neeraj Kaushik
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
Betclic Everest Group Tech Team
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
Andy Butland
 
Completable future
Completable futureCompletable future
Completable future
Srinivasan Raghvan
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
Bansilal Haudakari
 
Multiprocessing.pdf..............,.......
Multiprocessing.pdf..............,.......Multiprocessing.pdf..............,.......
Multiprocessing.pdf..............,.......
Bhaveshmali28
 
Multiprocessing.............,...........
Multiprocessing.............,...........Multiprocessing.............,...........
Multiprocessing.............,...........
Bhaveshmali28
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
Jim Bethancourt
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
Maxim Volgin
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PROIDEA
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
kshanth2101
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
Minh Tran
 
Modern_Java_Workshop manjunath np hj slave
Modern_Java_Workshop manjunath np hj slaveModern_Java_Workshop manjunath np hj slave
Modern_Java_Workshop manjunath np hj slave
gangadharnp111
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
Neeraj Kaushik
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
Andy Butland
 
Multiprocessing.pdf..............,.......
Multiprocessing.pdf..............,.......Multiprocessing.pdf..............,.......
Multiprocessing.pdf..............,.......
Bhaveshmali28
 
Multiprocessing.............,...........
Multiprocessing.............,...........Multiprocessing.............,...........
Multiprocessing.............,...........
Bhaveshmali28
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PROIDEA
 
Ad

More from Martin Toshev (20)

Building highly scalable data pipelines with Apache Spark
Building highly scalable data pipelines with Apache SparkBuilding highly scalable data pipelines with Apache Spark
Building highly scalable data pipelines with Apache Spark
Martin Toshev
 
Big data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle DatabaseBig data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle Database
Martin Toshev
 
Jdk 10 sneak peek
Jdk 10 sneak peekJdk 10 sneak peek
Jdk 10 sneak peek
Martin Toshev
 
Semantic Technology In Oracle Database 12c
Semantic Technology In Oracle Database 12cSemantic Technology In Oracle Database 12c
Semantic Technology In Oracle Database 12c
Martin Toshev
 
Practical security In a modular world
Practical security In a modular worldPractical security In a modular world
Practical security In a modular world
Martin Toshev
 
Java 9 Security Enhancements in Practice
Java 9 Security Enhancements in PracticeJava 9 Security Enhancements in Practice
Java 9 Security Enhancements in Practice
Martin Toshev
 
Java 9 sneak peek
Java 9 sneak peekJava 9 sneak peek
Java 9 sneak peek
Martin Toshev
 
Writing Stored Procedures in Oracle RDBMS
Writing Stored Procedures in Oracle RDBMSWriting Stored Procedures in Oracle RDBMS
Writing Stored Procedures in Oracle RDBMS
Martin Toshev
 
Spring RabbitMQ
Spring RabbitMQSpring RabbitMQ
Spring RabbitMQ
Martin Toshev
 
Security Architecture of the Java platform
Security Architecture of the Java platformSecurity Architecture of the Java platform
Security Architecture of the Java platform
Martin Toshev
 
Oracle Database 12c Attack Vectors
Oracle Database 12c Attack VectorsOracle Database 12c Attack Vectors
Oracle Database 12c Attack Vectors
Martin Toshev
 
JVM++: The Graal VM
JVM++: The Graal VMJVM++: The Graal VM
JVM++: The Graal VM
Martin Toshev
 
RxJS vs RxJava: Intro
RxJS vs RxJava: IntroRxJS vs RxJava: Intro
RxJS vs RxJava: Intro
Martin Toshev
 
Security Аrchitecture of Тhe Java Platform
Security Аrchitecture of Тhe Java PlatformSecurity Аrchitecture of Тhe Java Platform
Security Аrchitecture of Тhe Java Platform
Martin Toshev
 
Spring RabbitMQ
Spring RabbitMQSpring RabbitMQ
Spring RabbitMQ
Martin Toshev
 
Writing Stored Procedures with Oracle Database 12c
Writing Stored Procedures with Oracle Database 12cWriting Stored Procedures with Oracle Database 12c
Writing Stored Procedures with Oracle Database 12c
Martin Toshev
 
The RabbitMQ Message Broker
The RabbitMQ Message BrokerThe RabbitMQ Message Broker
The RabbitMQ Message Broker
Martin Toshev
 
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Martin Toshev
 
Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Modularity of The Java Platform Javaday (http://javaday.org.ua/)Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Martin Toshev
 
Writing Java Stored Procedures in Oracle 12c
Writing Java Stored Procedures in Oracle 12cWriting Java Stored Procedures in Oracle 12c
Writing Java Stored Procedures in Oracle 12c
Martin Toshev
 
Building highly scalable data pipelines with Apache Spark
Building highly scalable data pipelines with Apache SparkBuilding highly scalable data pipelines with Apache Spark
Building highly scalable data pipelines with Apache Spark
Martin Toshev
 
Big data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle DatabaseBig data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle Database
Martin Toshev
 
Semantic Technology In Oracle Database 12c
Semantic Technology In Oracle Database 12cSemantic Technology In Oracle Database 12c
Semantic Technology In Oracle Database 12c
Martin Toshev
 
Practical security In a modular world
Practical security In a modular worldPractical security In a modular world
Practical security In a modular world
Martin Toshev
 
Java 9 Security Enhancements in Practice
Java 9 Security Enhancements in PracticeJava 9 Security Enhancements in Practice
Java 9 Security Enhancements in Practice
Martin Toshev
 
Writing Stored Procedures in Oracle RDBMS
Writing Stored Procedures in Oracle RDBMSWriting Stored Procedures in Oracle RDBMS
Writing Stored Procedures in Oracle RDBMS
Martin Toshev
 
Security Architecture of the Java platform
Security Architecture of the Java platformSecurity Architecture of the Java platform
Security Architecture of the Java platform
Martin Toshev
 
Oracle Database 12c Attack Vectors
Oracle Database 12c Attack VectorsOracle Database 12c Attack Vectors
Oracle Database 12c Attack Vectors
Martin Toshev
 
RxJS vs RxJava: Intro
RxJS vs RxJava: IntroRxJS vs RxJava: Intro
RxJS vs RxJava: Intro
Martin Toshev
 
Security Аrchitecture of Тhe Java Platform
Security Аrchitecture of Тhe Java PlatformSecurity Аrchitecture of Тhe Java Platform
Security Аrchitecture of Тhe Java Platform
Martin Toshev
 
Writing Stored Procedures with Oracle Database 12c
Writing Stored Procedures with Oracle Database 12cWriting Stored Procedures with Oracle Database 12c
Writing Stored Procedures with Oracle Database 12c
Martin Toshev
 
The RabbitMQ Message Broker
The RabbitMQ Message BrokerThe RabbitMQ Message Broker
The RabbitMQ Message Broker
Martin Toshev
 
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Security Architecture of the Java Platform (BG OUG, Plovdiv, 13.06.2015)
Martin Toshev
 
Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Modularity of The Java Platform Javaday (http://javaday.org.ua/)Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Modularity of The Java Platform Javaday (http://javaday.org.ua/)
Martin Toshev
 
Writing Java Stored Procedures in Oracle 12c
Writing Java Stored Procedures in Oracle 12cWriting Java Stored Procedures in Oracle 12c
Writing Java Stored Procedures in Oracle 12c
Martin Toshev
 
Ad

Concurrency Utilities in Java 8

  • 2. 2CONFIDENTIAL Agenda • Parallel Tasks • Parallel Streams • Parallel Array operations
  • 3. 3CONFIDENTIAL Agenda • CompletableFuture • ConcurrentHashMap • Scalable Updatable Variables • StampedLock
  • 5. 5CONFIDENTIAL Parallel Tasks • Parallel tasks are represented by implementations of the ForkJoinTask that are submitted to a ForkJoinPool instance for execution • Provides a direct way to implement a divide-and-conquer mechanism for executing multiple tasks in parallel (in regard to executing many independent tasks with the ExecutorService)
  • 6. 6CONFIDENTIAL Parallel Tasks • Typical implementations of parallel tasks do not extend directly ForkJoinTask • RecursiveTask instances can be used to execute parallel tasks that return a result • RecursiveAction instances can be used to execute parallel tasks that do not return a result
  • 7. 7CONFIDENTIAL Parallel Tasks • ForkJoinPool.commonPool() - introduced in Java 8 in order to retrieve the common pool that is basically a fork-join pool for all ForkJoinTasks that are not submitted to a pool • The common pool is used to implement parallel streams and parallel array operations • The common pool is as easy way to retrieve a ForkJoinPool for parrallel operations that also improves resource utilization
  • 8. 8CONFIDENTIAL Parallel Tasks public class NumberFormatAction extends RecursiveAction { … @Override protected void compute() { if (end - start <= 2) { System.out.println(Thread.currentThread().getName() + " " + start + " " + end); } else { int mid = start + (end - start) / 2; NumberFormatAction left = new NumberFormatAction(start, mid); NumberFormatAction right = new NumberFormatAction(mid + 1, end); invokeAll(left, right); } public static void main(String[] args) { NumberFormatAction action = new NumberFormatAction(1, 50); ForkJoinPool.commonPool().invoke(action); }}}
  • 10. 10CONFIDENTIAL Parallel Streams • Streams (sequential and parallel) are introduced in java 8 with the utilities in the java.util.stream package • You can get a parallel stream from a collection using the parallelStream() method or convert a sequential stream to a parallel one using the parallel() method
  • 11. 11CONFIDENTIAL Parallel Streams Stream.of(1,2,3,4,5,6,7,8,9).parallel().forEach(System.out::println); Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} ).parallel().count(); LinkedList<Integer> list = new LinkedList<Integer>(); list.add(100); list.add(200); list.add(300); Stream<Integer> stream1 = list.stream().parallel(); Stream<Integer> stream2 = list.parallelStream(); int[] values = IntStream.range(1000, 2000).parallel().toArray(); Arrays.stream(new double[]{1.1, 2.2, 3.3}).parallel().sum();
  • 12. 12CONFIDENTIAL Parallel Streams • However … – Be careful when using parallel streams - they may be slower then sequential streams – Always do proper benchmarks (e.g. using JMH)
  • 13. 13CONFIDENTIAL Parallel Streams • Parallel streams may not be proper when: – A data structure that cannot be split well is used (e.g. LinkedList) – Tasks are not independent and they cannot be split independently – There are a lot of IO operations (file, network etc. ) or synchronization – Simple operations are performed that may be optimized when using a sequential stream
  • 14. 14CONFIDENTIAL Parallel Streams • Parallel streams may not be proper when: – A data structure that cannot be split well is used (e.g. LinkedList) – Tasks are not independent and they cannot be split independently – There are a lot of IO operations (file, network etc. ) or synchronization – Simple operations are performed that may be optimized when using a sequential stream (Some of the limitations are implied due to the shared common Fork/Join pool being used for parallel streams)
  • 15. 15CONFIDENTIAL Parallel Streams • Imagine a JavaEE application creating a parallel stream … • … and several other applications creating parallel streams • ALL of them use the common Fork/Join pool by default … => Simply do not create a parallel stream in a JavaEE application
  • 16. 16CONFIDENTIAL Parallel Streams public static void listStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.stream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } public static void listParallelStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.parallelStream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } VS
  • 17. 17CONFIDENTIAL Parallel Streams public static void listStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.stream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } public static void listParallelStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.parallelStream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } VS List is a LinkedList instance 64 ms 309 ms
  • 18. 18CONFIDENTIAL Parallel Streams public static void listStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.stream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } public static void listParallelStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.parallelStream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } VS List is an ArrayList instance 55 ms 280 ms
  • 19. 19CONFIDENTIAL Parallel Streams public static void listStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.stream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } public static void listParallelStream() { List<Integer> numbers = getRandomNumbers(10_000_000); long start = System.nanoTime(); numbers.parallelStream(). filter(num -> num%2 == 0).count(); long end = System.nanoTime(); System.out.println((end – start)/1000); } VS (second run for both methods) 6 ms 1 ms
  • 20. 20CONFIDENTIAL Parallel Streams • System.nanoTime() is the one of the most naïve (and incorrect) approaches for doing benchmarks in practices … • It does not take into account: – a warm up phase before timing (triggers all initializations and JIT compilations) – side work done by the JVM (such as GC, output from another threads …) – many other things depending on the type of performance statistics being gathered …
  • 21. 21CONFIDENTIAL Parallel Streams • Possible libraries you can use to measure performance of parallel streams include: – JMH – Caliper – JUnitBenchmarks
  • 22. 22CONFIDENTIAL Parallel Streams • The limitations imposed by the Fork/Join framework used by parallel streams leads to a rigid critique: “The JDK1.8 engineers are using the recursively decomposing F/J framework, with its very, very narrow measure, as the engine for parallel programming support simply because they don’t have anything else.”
  • 25. 25CONFIDENTIAL Parallel Array Operations • JDK 8 introduces a number of new methods in the Arrays utility that allow parallel manipulation of arrays • The methods are divided in three categories: – parallelSort() – sorts an array in parallel – parallelPrefix() – performs a cumulative operations on an array in parallel – parallelSetAll() – sets all of the elements in an array in parallel
  • 26. 26CONFIDENTIAL Parallel Streams Integer[] array = { 16, 7, 26, 14, 77 }; Arrays.parallelSort(array); [7, 14, 16, 26, 77] Integer[] array = { 16, 7, 26, 14, 77 }; Arrays.parallelPrefix(array, (a, b) -> a * b); [16, 112, 2912, 40768, 3139136] Integer[] array = new Integer[7]; Arrays.parallelSetAll(array, i -> i); [0, 1, 2, 3, 4, 5, 6]
  • 28. 28CONFIDENTIAL CompletableFuture • Provides a facility to create a chain of dependent non-blocking tasks - an asynchronous task can be triggered as the result of a completion of another task • A CompletableFuture may be completed/cancelled by a thread prematurely • Such a facility is already provided by Google's Guava library Task 1 Task 2 Task 3 Task 4triggers triggers triggers
  • 29. 29CONFIDENTIAL CompletableFuture • Provides a very flexible API that allows additionally to: o combine the result of multiple tasks in a CompletableFuture o provide synchronous/asynchronous callbacks upon completion of a task o provide a CompletableFuture that executes when first task in group completes o provide a CompletableFuture that executes when all tasks in a group complete
  • 30. 30CONFIDENTIAL CompletableFuture CompletableFuture<Integer> task1 = new CompletableFuture<Integer>(); // forcing completing of future by specifying result task1.complete(10);
  • 31. 31CONFIDENTIAL CompletableFuture CompletableFuture<Integer> task1 = CompletableFuture .supplyAsync(() -> { … return 10;}); // executed on completion of the future task1.thenApply((x) -> {…}); // executed in case of exception or completion of the future task1.handle((x, y) -> {…}); // can be completed prematurely with a result // task1.complete(20); System.err.println(task1.get());
  • 32. 32CONFIDENTIAL CompletableFuture CompletableFuture<Object> prev = null; Supplier<Object> supplier = () -> { … }; for (int i = 0; i < count; i++) { CompletableFuture<Object> task; if (prev != null) { task = prev.thenCompose((x) -> { return CompletableFuture.supplyAsync(supplier);}); } else { task = CompletableFuture.supplyAsync(supplier); } prev = task; } prev.get();
  • 34. 34CONFIDENTIAL ConcurrentHashMap • ConcurrentHashMap<V, K> class completely rewritten in order to improve its usage as a cache and several new methods have been added as part of the new stream and lambda expressions: o forEach o forEachKey o forEachValue o forEachEntry
  • 36. 36CONFIDENTIAL Scalable Updatable Variables • Maintaining a single variable that is updatable from many threads is a common scalability issue • Atomic variables already present in the JDK serve as a means to implement updatable variables in a multithreaded environment • New classes are introduced in order to reduce atomicity guarantees in favor of high throughput - DoubleAccumulator, DoubleAdder, LongAccumulator, LongAdder
  • 37. 37CONFIDENTIAL DoubleAccumulator accumulator = new DoubleAccumulator((x, y) -> x + y, 0.0); // code being executed from some threads accumulator.accumulate(10); accumulator.accumulate(20); System.out.println(accumulator.doubleValue()); Scalable Updatable Variables
  • 39. 39CONFIDENTIAL StampedLock • A very specialized type of explicit lock • Similar to ReentrantReadWriteLock but provides additionally conversion between the lock modes (writing, reading and optimistic reading) • Optimistic read lock is a "cheap" version of a read lock that can be invalidated by a read lock • Lock state is determined by version and lock mode
  • 40. 40CONFIDENTIAL StampedLock • Example StampedLock sl = new StampedLock(); long stamp = sl.writeLock(); try { // do something that needs exclusive locking } finally { sl.unlockWrite(stamp); }
  • 41. 41CONFIDENTIAL StampedLock • Example public long getValue() { long stamp = sl.tryOptimisticRead(); long value = this.value; if (!sl.validate(stamp)) { stamp = sl.readLock(); try { value = this.value; } finally { sl.unlockRead(stamp); } } return value; }
  • 43. 43CONFIDENTIAL Resources Concurrency Utility Enhancements in Java 8 https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/changes8.html JVM concurrency: Java 8 concurrency basics http://www.ibm.com/developerworks/library/j-jvmc2/index.html Everything about Java 8 http://www.techempower.com/blog/2013/03/26/everything-about-java-8/ Java Specialists newsletter http://www.javaspecialists.eu/archive/archive.jsp
  • 44. 44CONFIDENTIAL Resources Java Tutorials: Parallelism https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html Think twice before using Java 8 parallel streams http://java.dzone.com/articles/think-twice-using-java-8 Parallel operations in Java 8 http://www.drdobbs.com/jvm/parallel-array-operations-in-java-8/240166287 Package java.util.stream https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html How do I write a correct microbenchmark in Java http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java
  • 45. 45CONFIDENTIAL Resources Fork/Join Framework Tutorial: ForkJoinPool Example http://howtodoinjava.com/2014/05/27/forkjoin-framework-tutorial-forkjoinpool-example/ Java 8 tutorial: Streams by Examples http://howtodoinjava.com/2014/04/13/java-8-tutorial-streams-by-examples/ When to use parallel streams http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html A Java Fork/Join Calamity http://www.coopsoft.com/ar/CalamityArticle.html A Java Parallel Calamity http://www.coopsoft.com/ar/Calamity2Article.html
  • 46. 46CONFIDENTIAL Resources Java Parallel Stream Performance http://stackoverflow.com/questions/22999188/java-parallel-stream-performance Java Theory and practice: Anatomy of a flawed microbenchmark https://www.ibm.com/developerworks/java/library/j-jtp02225/ Java 8 Concurrency Tutorial: Synchronization and Locks http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/