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

More Related Content

What's hot (20)

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

Similar to Java concurrency (20)

KEY
Modern Java Concurrency
Ben Evans
 
KEY
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
DOCX
Java 5 concurrency
priyank09
 
PDF
Concurrent Programming in Java
Lakshmi Narasimhan
 
PDF
Working With Concurrency In Java 8
Heartin Jacob
 
KEY
Modern Java Concurrency (Devoxx Nov/2011)
Martijn Verburg
 
PDF
Concurrency on the JVM
Bernhard Huemer
 
PPTX
Concurrency in Java
Allan Huang
 
PPT
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
JAX London
 
DOC
Concurrency Learning From Jdk Source
Kaniska Mandal
 
PPTX
Introduction to Concurrent Data Structures
Dilum Bandara
 
PDF
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
PDF
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
ODP
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
PDF
Java 8 features
Oleg Tsal-Tsalko
 
ODP
Java Concurrency
Carol McDonald
 
PDF
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Subhajit Sahu
 
PDF
What is new in java 8 concurrency
kshanth2101
 
PPTX
Why Concurrency is hard ?
Ramith Jayasinghe
 
PPTX
Concurrency - Why it's hard ?
Ramith Jayasinghe
 
Modern Java Concurrency
Ben Evans
 
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
Java 5 concurrency
priyank09
 
Concurrent Programming in Java
Lakshmi Narasimhan
 
Working With Concurrency In Java 8
Heartin Jacob
 
Modern Java Concurrency (Devoxx Nov/2011)
Martijn Verburg
 
Concurrency on the JVM
Bernhard Huemer
 
Concurrency in Java
Allan Huang
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
JAX London
 
Concurrency Learning From Jdk Source
Kaniska Mandal
 
Introduction to Concurrent Data Structures
Dilum Bandara
 
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
Java 8 features
Oleg Tsal-Tsalko
 
Java Concurrency
Carol McDonald
 
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Subhajit Sahu
 
What is new in java 8 concurrency
kshanth2101
 
Why Concurrency is hard ?
Ramith Jayasinghe
 
Concurrency - Why it's hard ?
Ramith Jayasinghe
 
Ad

Recently uploaded (20)

PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
July Patch Tuesday
Ivanti
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
July Patch Tuesday
Ivanti
 
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 .