SlideShare a Scribd company logo
Java Concurrency
Towards a better parallel balanced life …
● By
● - Srinivasan.raghavan@oracle.com
- Vaibhav.x.choudhary@oracle.com
Java Releases
JDK 1.5:
- Generics
-Concurrency
-Auto-boxing
J2SE 6:
- Collection Framework
enhancement
-Drag and Drop
-Improve IO support
J2SE 7:
- Concurrency Utilities
- Swing, -Fork/Join Pool ,
LWC&HWC
- G1
J2SE 8:
- Lambda expressions
- Stream Package
-More concurrency
Updates
Agenda
● Basics of concurrency
● Java Concurrent Package
● Reentanrant Lock / Performance
● Concurrent Collections
● Insight of Fork/Join
● Concurrency in Java 8
● Concurrent Collections java 8 updates
Basics of concurrency
● Amdhal's law
– Speedup <= 1/ (F + (1-F)/N)
● Age of multi-core processors and requirement
of parallel programming in mainstream.
● Writing concurrent programing is hard and
writing correct concurrent program is even
harder.
● Research in the field has been lead by Java
designers and the concurrency interest group
General Design Principles

Its the mutable state stupid

Design for immutability

Make field final unless they need to be mutable

Encapsulate to manage complexity

Guard all variables in an invariant with same
lock

Document the synchronization policy
Java Concurrency Constructs
● In Java 5 a set of synchronisers was built giving
more power than intrinsic locking
● Atomic operations using Compare and swap
● Non-Blocking Collections
Intrinsic locks vs Reentarant Locks
● Intrinsic locks are very low level constructs
● Reentarant Locks offers more features
● Fair queuing
● TryLock
● Explicit Condition Queue
Dead Locks avoidance with try locks

Dead Lock can be avoided using the try locks
●
Try locks can used for Probabilistic lock acquire
of the lock and exponential back-off and spin lock
●
Classic Transfer money Deadlock Problem
Typical traffic problem.
Condition Queues
●
Intrinsic locks can also work as condition queues
with wait and notify
●
Problem occurs when Intrinsic condition queue
may be used with more than one condition
predicate
●
Reentarant locks provides explicit condition
queues which offers more features
●
Fairness in access of the lock which reduce
contention
●
Help us create queues per condition predicate
Condition Queues
● Where a Lock replaces the use of synchronized
methods and statements, a Condition replaces
the use of the Object monitor methods.
● Consider a case of bounded buffer
– final Condition notFull = lock.newCondition();
– final Condition notEmpty = lock.newCondition();
● ArrayBlockingQueue provides the functionality
● A typical consumer/producer problem with
BlockingQueue.
Performance
Can Reentarant lock replace synchronized?
– No , There are big issues when used carelessly
– Use only when Fair queuing, probabilistic locking
and explicit condition queues are required
Java Synchronizers
● Executors
– Execution tasks in thread pools.
– Cyclic barrier ,Future task ,Semaphores
,Countdown latch
● Non-blocking algorithms
– AtomicInteger
– Compare and Swap (CAS)
– Writing a correct non-blocking algorithm are tough.
● Find out the code.
Concurrent Collection
● Copy on write array List
● Concurrent hash-map
● Blocking queues and producer – consumer
pattern
Fork/Join Framework
● New parallel mechanism for compute intensive
tasks, the fork-join framework.
● Runs on work-stealing algorithm.
● Approach
– Partition into subproblems.
– Create subtaks.
– Fork subtasks.
– Join subtaks.
– Compose solution.
Fork/Join Framework
Java 8 way
Parallelism Built In
● Parallelism is the feature built in Java 8
● U can iterate,filter, sort ,map reduce an array in
parallel
● The streams api makes heavy use of the Fork-
Join framework
● The entire set of features offered by the streams
api can be made to work in parallel by just
asking for the parallel stream
● The inherit complexity of Fork-Join framework is
handled under the hood
Java 8 Concurrency Changes
● Addition of fork-join common pool
● ConcurrentHashmapv8 which supports bulk
operations in parallel
● Completable future
● Changes to the Future Task implementation
Concurrent Hashmap
● Started as a replacement to Hashtable.
● Implements Lock stripping
● Doesn’t throw concurrent modification
exception
● Java 8 concurrenthashmap does a lot
● Built in support for parallelism
● More in the code .
Java concurrency
Ad

Recommended

Introduction to Reactjs
Introduction to Reactjs
NodeXperts
 
Building Asynchronous Microservices with Armeria
Building Asynchronous Microservices with Armeria
LINE Corporation
 
Java 8 parallel stream
Java 8 parallel stream
Yung Chieh Tsai
 
JAVA 8 Parallel Stream
JAVA 8 Parallel Stream
Tengwen Wang
 
Gatling Tool in Action at Devoxx 2012
Gatling Tool in Action at Devoxx 2012
slandelle
 
React october2017
React october2017
David Greenfield
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015
Eran Harel
 
Migrating our micro services from Java to Kotlin (Code.Talks 2018)
Migrating our micro services from Java to Kotlin (Code.Talks 2018)
Björn Wendland
 
Advance java session 15
Advance java session 15
Smita B Kumar
 
Performance Test Automation With Gatling
Performance Test Automation With Gatling
Knoldus Inc.
 
Java concurrency
Java concurrency
fbenault
 
Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012
slandelle
 
Gatling
Gatling
Swapnil Kotwal
 
Rubyhosting
Rubyhosting
Artit Rubybox
 
Three Lessons about Gatling and Microservices
Three Lessons about Gatling and Microservices
Dragos Manolescu
 
Schematron & SQF In oXygen
Schematron & SQF In oXygen
Octavian Nadolu
 
Building RESTtful services in MEAN
Building RESTtful services in MEAN
Madhukara Phatak
 
GraalVM
GraalVM
NexThoughts Technologies
 
Thinking in Swift
Thinking in Swift
Martin Mitrevski
 
Gatling
Gatling
Gaurav Shukla
 
Schematron QuickFix
Schematron QuickFix
Octavian Nadolu
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUG
slandelle
 
Byte code engineering 21st May Saturday 2016
Byte code engineering 21st May Saturday 2016
Sarath Soman
 
Functional? Reactive? Why?
Functional? Reactive? Why?
Aleksandr Tavgen
 
Load test REST APIs using gatling
Load test REST APIs using gatling
Jayaram Sankaranarayanan
 
Continuous performance management with Gatling
Continuous performance management with Gatling
Radoslaw Smilgin
 
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Concurrency in Java
Concurrency in Java
Lakshmi Narasimhan
 
Why Concurrency is hard ?
Why Concurrency is hard ?
Ramith Jayasinghe
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
Ramith Jayasinghe
 

More Related Content

What's hot (20)

Advance java session 15
Advance java session 15
Smita B Kumar
 
Performance Test Automation With Gatling
Performance Test Automation With Gatling
Knoldus Inc.
 
Java concurrency
Java concurrency
fbenault
 
Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012
slandelle
 
Gatling
Gatling
Swapnil Kotwal
 
Rubyhosting
Rubyhosting
Artit Rubybox
 
Three Lessons about Gatling and Microservices
Three Lessons about Gatling and Microservices
Dragos Manolescu
 
Schematron & SQF In oXygen
Schematron & SQF In oXygen
Octavian Nadolu
 
Building RESTtful services in MEAN
Building RESTtful services in MEAN
Madhukara Phatak
 
GraalVM
GraalVM
NexThoughts Technologies
 
Thinking in Swift
Thinking in Swift
Martin Mitrevski
 
Gatling
Gatling
Gaurav Shukla
 
Schematron QuickFix
Schematron QuickFix
Octavian Nadolu
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUG
slandelle
 
Byte code engineering 21st May Saturday 2016
Byte code engineering 21st May Saturday 2016
Sarath Soman
 
Functional? Reactive? Why?
Functional? Reactive? Why?
Aleksandr Tavgen
 
Load test REST APIs using gatling
Load test REST APIs using gatling
Jayaram Sankaranarayanan
 
Continuous performance management with Gatling
Continuous performance management with Gatling
Radoslaw Smilgin
 
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Concurrency in Java
Concurrency in Java
Lakshmi Narasimhan
 

Similar to Java concurrency (20)

Why Concurrency is hard ?
Why Concurrency is hard ?
Ramith Jayasinghe
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
Ramith Jayasinghe
 
NetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talks
Ruslan Meshenberg
 
What’s expected in Java 9
What’s expected in Java 9
Gal Marder
 
ForkJoinPools and parallel streams
ForkJoinPools and parallel streams
Oresztész Margaritisz
 
Concurrent Programming in Java
Concurrent Programming in Java
Lakshmi Narasimhan
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
Modern Java Concurrency
Modern Java Concurrency
Ben Evans
 
Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012
mumrah
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
Mohanraj Thirumoorthy
 
Comparison between zookeeper, etcd 3 and other distributed coordination systems
Comparison between zookeeper, etcd 3 and other distributed coordination systems
Imesha Sudasingha
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
jexp
 
Java under the hood
Java under the hood
Vachagan Balayan
 
Kotlin REST & GraphQL API
Kotlin REST & GraphQL API
Sean O'Brien
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
Ramith Jayasinghe
 
NetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talks
Ruslan Meshenberg
 
What’s expected in Java 9
What’s expected in Java 9
Gal Marder
 
Concurrent Programming in Java
Concurrent Programming in Java
Lakshmi Narasimhan
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
Modern Java Concurrency
Modern Java Concurrency
Ben Evans
 
Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012
mumrah
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
Mohanraj Thirumoorthy
 
Comparison between zookeeper, etcd 3 and other distributed coordination systems
Comparison between zookeeper, etcd 3 and other distributed coordination systems
Imesha Sudasingha
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
jexp
 
Kotlin REST & GraphQL API
Kotlin REST & GraphQL API
Sean O'Brien
 
Ad

Recently uploaded (20)

Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Ad

Java concurrency

  • 1. Java Concurrency Towards a better parallel balanced life … ● By ● - [email protected] - [email protected]
  • 2. Java Releases JDK 1.5: - Generics -Concurrency -Auto-boxing J2SE 6: - Collection Framework enhancement -Drag and Drop -Improve IO support J2SE 7: - Concurrency Utilities - Swing, -Fork/Join Pool , LWC&HWC - G1 J2SE 8: - Lambda expressions - Stream Package -More concurrency Updates
  • 3. Agenda ● Basics of concurrency ● Java Concurrent Package ● Reentanrant Lock / Performance ● Concurrent Collections ● Insight of Fork/Join ● Concurrency in Java 8 ● Concurrent Collections java 8 updates
  • 4. Basics of concurrency ● Amdhal's law – Speedup <= 1/ (F + (1-F)/N) ● Age of multi-core processors and requirement of parallel programming in mainstream. ● Writing concurrent programing is hard and writing correct concurrent program is even harder. ● Research in the field has been lead by Java designers and the concurrency interest group
  • 5. General Design Principles  Its the mutable state stupid  Design for immutability  Make field final unless they need to be mutable  Encapsulate to manage complexity  Guard all variables in an invariant with same lock  Document the synchronization policy
  • 6. Java Concurrency Constructs ● In Java 5 a set of synchronisers was built giving more power than intrinsic locking ● Atomic operations using Compare and swap ● Non-Blocking Collections
  • 7. Intrinsic locks vs Reentarant Locks ● Intrinsic locks are very low level constructs ● Reentarant Locks offers more features ● Fair queuing ● TryLock ● Explicit Condition Queue
  • 8. Dead Locks avoidance with try locks  Dead Lock can be avoided using the try locks ● Try locks can used for Probabilistic lock acquire of the lock and exponential back-off and spin lock ● Classic Transfer money Deadlock Problem Typical traffic problem.
  • 9. Condition Queues ● Intrinsic locks can also work as condition queues with wait and notify ● Problem occurs when Intrinsic condition queue may be used with more than one condition predicate ● Reentarant locks provides explicit condition queues which offers more features ● Fairness in access of the lock which reduce contention ● Help us create queues per condition predicate
  • 10. Condition Queues ● Where a Lock replaces the use of synchronized methods and statements, a Condition replaces the use of the Object monitor methods. ● Consider a case of bounded buffer – final Condition notFull = lock.newCondition(); – final Condition notEmpty = lock.newCondition(); ● ArrayBlockingQueue provides the functionality ● A typical consumer/producer problem with BlockingQueue.
  • 11. Performance Can Reentarant lock replace synchronized? – No , There are big issues when used carelessly – Use only when Fair queuing, probabilistic locking and explicit condition queues are required
  • 12. Java Synchronizers ● Executors – Execution tasks in thread pools. – Cyclic barrier ,Future task ,Semaphores ,Countdown latch ● Non-blocking algorithms – AtomicInteger – Compare and Swap (CAS) – Writing a correct non-blocking algorithm are tough. ● Find out the code.
  • 13. Concurrent Collection ● Copy on write array List ● Concurrent hash-map ● Blocking queues and producer – consumer pattern
  • 14. Fork/Join Framework ● New parallel mechanism for compute intensive tasks, the fork-join framework. ● Runs on work-stealing algorithm. ● Approach – Partition into subproblems. – Create subtaks. – Fork subtasks. – Join subtaks. – Compose solution.
  • 17. Parallelism Built In ● Parallelism is the feature built in Java 8 ● U can iterate,filter, sort ,map reduce an array in parallel ● The streams api makes heavy use of the Fork- Join framework ● The entire set of features offered by the streams api can be made to work in parallel by just asking for the parallel stream ● The inherit complexity of Fork-Join framework is handled under the hood
  • 18. Java 8 Concurrency Changes ● Addition of fork-join common pool ● ConcurrentHashmapv8 which supports bulk operations in parallel ● Completable future ● Changes to the Future Task implementation
  • 19. Concurrent Hashmap ● Started as a replacement to Hashtable. ● Implements Lock stripping ● Doesn’t throw concurrent modification exception ● Java 8 concurrenthashmap does a lot ● Built in support for parallelism ● More in the code .