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 Towards a Better Parallel Life

More Related Content

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 Towards a Better Parallel Life

Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
NetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talksNetflixOSS Open House Lightning talks
NetflixOSS Open House Lightning talksRuslan Meshenberg
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9Gal Marder
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaLakshmi Narasimhan
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Martijn Verburg
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java ConcurrencyBen Evans
 
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, 2012mumrah
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
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 GuideMohanraj Thirumoorthy
 
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 systemsImesha Sudasingha
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performancewhite paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performancewhite paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performancewhite paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performancewhite paper
 
Java Standard Edition 6 Performance
Java Standard Edition 6 PerformanceJava Standard Edition 6 Performance
Java Standard Edition 6 Performancewhite paper
 
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.pdfjexp
 
Kotlin REST & GraphQL API
Kotlin REST & GraphQL APIKotlin REST & GraphQL API
Kotlin REST & GraphQL APISean O'Brien
 

Similar to Java Concurrency Towards a Better Parallel Life (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

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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 Processorsdebabhi2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Java Concurrency Towards a Better Parallel Life

  • 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 .