SlideShare a Scribd company logo
1 of 20
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

Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012
slandelle
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUG
slandelle
 

What's hot (20)

Advance java session 15
Advance java session 15Advance java session 15
Advance java session 15
 
Performance Test Automation With Gatling
Performance Test Automation  With GatlingPerformance Test Automation  With Gatling
Performance Test Automation With Gatling
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012Hands On, Duchess 10/17/2012
Hands On, Duchess 10/17/2012
 
Gatling
GatlingGatling
Gatling
 
Rubyhosting
RubyhostingRubyhosting
Rubyhosting
 
Three Lessons about Gatling and Microservices
Three Lessons about Gatling and MicroservicesThree Lessons about Gatling and Microservices
Three Lessons about Gatling and Microservices
 
Schematron & SQF In oXygen
Schematron & SQF In oXygenSchematron & SQF In oXygen
Schematron & SQF In oXygen
 
Building RESTtful services in MEAN
Building RESTtful services in MEANBuilding RESTtful services in MEAN
Building RESTtful services in MEAN
 
GraalVM
GraalVMGraalVM
GraalVM
 
Thinking in Swift
Thinking in SwiftThinking in Swift
Thinking in Swift
 
Gatling
Gatling Gatling
Gatling
 
Schematron QuickFix
Schematron QuickFixSchematron QuickFix
Schematron QuickFix
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUG
 
Byte code engineering 21st May Saturday 2016
Byte code engineering   21st May Saturday 2016Byte code engineering   21st May Saturday 2016
Byte code engineering 21st May Saturday 2016
 
Functional? Reactive? Why?
Functional? Reactive? Why?Functional? Reactive? Why?
Functional? Reactive? Why?
 
Load test REST APIs using gatling
Load test REST APIs using gatlingLoad test REST APIs using gatling
Load test REST APIs using gatling
 
Continuous performance management with Gatling
Continuous performance management with GatlingContinuous performance management with Gatling
Continuous performance management with Gatling
 
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter LehtoJavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 

Similar to Java concurrency

Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
white paper
 

Similar to Java concurrency (20)

Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
NetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talksNetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talks
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9
 
ForkJoinPools and parallel streams
ForkJoinPools and parallel streamsForkJoinPools and parallel streams
ForkJoinPools and parallel streams
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
 
Comparison between zookeeper, etcd 3 and other distributed coordination systems
Comparison between zookeeper, etcd 3 and other distributed coordination systemsComparison between zookeeper, etcd 3 and other distributed coordination systems
Comparison between zookeeper, etcd 3 and other distributed coordination systems
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performance
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
 
Java under the hood
Java under the hoodJava under the hood
Java under the hood
 
Kotlin REST & GraphQL API
Kotlin REST & GraphQL APIKotlin REST & GraphQL API
Kotlin REST & GraphQL API
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Java concurrency

  • 1. Java Concurrency Towards a better parallel balanced life … ● By ● - Srinivasan.raghavan@oracle.com - Vaibhav.x.choudhary@oracle.com
  • 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 .