SlideShare a Scribd company logo
Java & Concurrency
Lakshmi Narasimhan
2
Agenda
 United at birth
 Why more relevant now
 Concurrency over the years
 Basics & Myths
 Concurrency Patterns
 Deep-dive
 There's more than one way to solve it!!
 Common gotchas
 Q&A
3
Java & Concurrency : United at birth
●
In-built into the language
●
Not an after thought
●
Support for threads from Day 1
●
Everything is a monitor
●
Simplicity over complexity
●
Multiple inheritance
●
Operator overloading
●
Choice of languages
●
Scala, Clojure
4
Why is it more relevant now
• H/W & S/W trends
– No longer privilege of few
– Scaling accentuates Vertical and horizontal scaling mean more
complexity
– Distributed caches, faster I/O
– Weak Cache coherence
• Application requirements
– Impunity of slow system lifted
– Elasticity is the mantra
– The weakest link draws the whole system down
– Bottlenecks can be really really costly
5
Concurrency over the years
Java VersionJava Version FeaturesFeatures
JDK 1.0 ●Threads
●“synchronized” keyword
●HashTable(thread-safe but synchronized classes)
JDK 1.2 ●Collections framework(updated)
●un-synchronized classes bound by
synchronized accessors
●eg: Collections.synchronizedMap()
●Use when you need sync
JDK 1.5 ● concurrency package JSR166 java.util.concurrent
JDK 7 ● Updated java.util.concurrent package(JSR166y)
Fork-Join and Executor Framework
JDK 8 ● Modularity
● Lamda
6
Concurrency : Basics
Three elements of a concurrent Application

Atomicity

Certain pieces of an application must all be executed as one unit
●
Visibility

Changes that you make to a value to be visible precisely when you intend them to
be
●
Synchronization

Concurrent access to shared variables should be guarded
Terms you will come across
●
JMM – The specification that guarantees how the JVM should work
●
Monitor – Intrinsic lock (Every object has a monitor associated with it)
●
Race condition – An operation on shared resources whose outcome may be in-
deterministic
●

7
Concurrency : Myths
●
The more the threads, faster my application is
• A bigger system means faster execution
• Processors/compilers can optimize my code for
parallelism
• Locks make the program go slow
• Re-factoring is easy
• Writing concurrent application is a black-art
8
Common concurrency patterns
Dynamic Exclusion Through Implicit Locks
• a.k.a “synchronized”
• Lock obtained if method is declared synchronized
• Watch out for fine/coarse grained locks.
• Dos/Dont's:
– Avoid CPU/IO intensive operations inside “synchronized” block.
– “synchronize” on a code block and not method (if appropriate)
• Remember that JIT can re-order execution
9
Common concurrency patterns
Structural Exclusion Through Confinement
• Options:
– Thread confinement : Access object from a single thread using
thread-per-session approach
– Instance confinement : Use encapsulation techniques to restrict
access to object state from multiple threads
– Method confinement : Do not allow an object to escape from
method by referencing it through local variables
• Do's/Dont's:
– Confinement cannot be relied on unless a design is leak proof
– Combine confinement with appropriate locking discipline
– Use ThreadLocal variables to maintain Thread Confinement
10
Common concurrency patterns
Immutability
• Object state cannot change after construction
• Are automatically thread-safe
• Immutability is guranteed only when :
– All fields are declared final
– Object reference fields must not allow modifications anywhere in the object
graph reachable from the fields after construction
– The class should be declared final (to prevent a subclass from subverting
these rules)
• Immutable objects eliminate a bunch of issues around concurrency. Use whenever you
can.
• Have “failure atomicity” - upon an exception, state is never undesirable or
indeterministic.
"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot
be made immutable, limit its mutability as much as possible." - Joshua Blochbe made immutable, limit its mutability as much as possible." - Joshua Bloch
11
Common concurrency patterns
Co-operation
• Wait-notify mechanisms
• One thread creates a condition and another one waits for it
• All threads are treated the same - when multiple threads are waiting,
no guarantee on which one would wake up
• Missed or early notifications
• Do's/Dont's:
– Use notifyAll() instead of notify()
– Wait() in a while loop
– Call wait() and notify() inside synchronized block
12
Deep Dive
• JSR166 - “java.util.concurrent” packages
• Introduced in JDK 5
• Motivation
– Creating APIs for commonly used functionality
– Application/scenario agnostic
– “volatile” guaranteed visibility but not atomicity
– Synchronization is expensive (and can cause dead-lock)
●
Atomic Variables, nanosecond timing, Condition variables, Lock classes,
tryLock, Queues, Barriers, Executors.....
●
13
Deep Dive – Atomic Classes
• java.util.concurrent.atomic.Atomic* classes
●
Provides a worthy substitute for “volatile” and “synchronized”
●
Available on Integer, Long, Boolean, Object Ref, Arrays
●
CompareAndSet(), incrementAndGet() etc guarantee atomicity
14
Deep Dive : Locks
ReentrantLock

“synchronization” on steroids

Supports Lock state, non-blocking tryLock() and interruptible locking

Throughput can be higher by order of magnitudes

Can be extended to set “fairness” and “Condition”







Use ONLY and ONLY if “synchronized” will not solve your problems

If your Application needs timed-locks, interruptible locks, multiple condition locks
15
Deep Dive : Executor Framework
●
Implemented via java.util.concurrent.ExecutorService
●
Provides flexible thread pool implementation
●
Producer-consumer pattern
– Producer : Creates work
– Consumer : Executes the work
●
●
●
●
●
Consider newCachedThreadPool if you don't want to bound no. of threads
16
Deep Dive : Executor Framework
●
Remember to shut it down
●
Asynchronous execution may hide hidden tasks
●
17
Deep Dive: Fork Join Framework
“Framework for supporting a style of programming in which problems are solved by recursively splitting them in
to sub tasks that are solved in parallel, waiting for them to complete and then composing results”
●
Introduced through JSR166y and part of Java 7
●
Introduces Parallel-Array (PA) concept to Java (simlar to Map-Reduce?)
●
Extends the executor framework for Recursive style problems
●
Makes use of work stealing algorithm
●
Transparent Parallelism requiring no tuning (almost!)
●
Suited for MMP Architectures.
●
Number of sub tasks can be altered at run-time

“Fork” Starts a new parallel fork/join subtask

“Join” causes current task not to proceed until the forked task is complete

This can happen recursively till tasks are smaller enough to be solved sequentially
Components of a FJ framework
●
The guy who does the work (business logic)
●
The guy who tells how the work can be split
●
The guy who coordinates it all (FJ)
18
There's more than one way to solve it
●
There's always more than one way to solve the problem
●
Know the pros/cons of each approach
●
Example :
Not Thread Safe
19
Option 1 : Using “volatile” keyword
Pros:
●
Simple, easy to write and understand
●
Would solve most of the requirement
Cons:
●
Thread looping
●
No option to know/set priority of waiting threads
There's more than one way to solve it
20
Option 2 : Using “AtomicBoolean”
Pros:
●
Fairly straight-forward
●
“tryLock()” can prevent costly thread spins
●
Flexibility to set Condition and “fairness” of waiting threads
Cons:
●
Slight over-head if you need primitive data locking
There's more than one way to solve it
21
Option 3 : Using “synchronized”
Pros:
●
Locks are intrinsic and so no need to release them explicitly
Cons
●
Code is a lot more verbose
●
Empty thread looping
There's more than one way to solve it
22
Common concurrency mistakes
●
synchronization
– Fine/coarse grained synchronization
– Synchronizing just data
– Validating data integrity at one level
– Synchronizing on “null”
– Changing synchronizing object instance
– Synchronizing on string literals
– Writing with synchronization but reading w/o it
– Volatile array -
• “valarr” is a volatile reference to an array, but array elements are not
23
Common concurrency mistakes
●
Volatile will work only when
– write to variable does not depend on current value
– Write does not depend on any other non-atomic operation
●
CachedThreadPool is un-bound – You may run out of resources
sooner than you thought
●
●
The list never ends.....
●
Recommend go through “Java Platform Concurrency Gotchas”
- Alex Miller
●
24
Re-cap
 Relevance of concurrency
 Basics & Myths
 Concurrency Patterns
 Deep-dive
 There's more than one way to solve it!!
 Common gotchas
 Q&A

More Related Content

What's hot

Java modularization
Java modularizationJava modularization
Java modularizationJonathan Vila
 
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Edureka!
 
Maximizing Throughput and Reliability with Pipelined Tasks
Maximizing Throughput and Reliability with Pipelined TasksMaximizing Throughput and Reliability with Pipelined Tasks
Maximizing Throughput and Reliability with Pipelined TasksPhillip Koza
 
Scotas - Oracle Open World Sao Pablo
Scotas - Oracle Open World Sao PabloScotas - Oracle Open World Sao Pablo
Scotas - Oracle Open World Sao PabloJulian Arocena
 
Java Presentation
Java PresentationJava Presentation
Java PresentationAmr Salah
 
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim WardJava EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim WardJAX London
 
CodeCamp Iasi 10 march 2012 - SolvingThePuzzle
CodeCamp Iasi 10 march 2012 - SolvingThePuzzleCodeCamp Iasi 10 march 2012 - SolvingThePuzzle
CodeCamp Iasi 10 march 2012 - SolvingThePuzzleCodecamp Romania
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Jax london 2011
Jax london 2011Jax london 2011
Jax london 2011njbartlett
 
What's new in Java EE 6
What's new in Java EE 6What's new in Java EE 6
What's new in Java EE 6Gal Marder
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platformsIlio Catallo
 
Double checkedlockingjavasingletons
Double checkedlockingjavasingletonsDouble checkedlockingjavasingletons
Double checkedlockingjavasingletonsparag
 
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011Arun Gupta
 
Eureka moment
Eureka momentEureka moment
Eureka momentPaul Withers
 

What's hot (18)

Java modularization
Java modularizationJava modularization
Java modularization
 
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
 
Modular Java
Modular JavaModular Java
Modular Java
 
Modularization in java 8
Modularization in java 8Modularization in java 8
Modularization in java 8
 
Maximizing Throughput and Reliability with Pipelined Tasks
Maximizing Throughput and Reliability with Pipelined TasksMaximizing Throughput and Reliability with Pipelined Tasks
Maximizing Throughput and Reliability with Pipelined Tasks
 
Scotas - Oracle Open World Sao Pablo
Scotas - Oracle Open World Sao PabloScotas - Oracle Open World Sao Pablo
Scotas - Oracle Open World Sao Pablo
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim WardJava EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
 
Lec 3 01_aug13
Lec 3 01_aug13Lec 3 01_aug13
Lec 3 01_aug13
 
CodeCamp Iasi 10 march 2012 - SolvingThePuzzle
CodeCamp Iasi 10 march 2012 - SolvingThePuzzleCodeCamp Iasi 10 march 2012 - SolvingThePuzzle
CodeCamp Iasi 10 march 2012 - SolvingThePuzzle
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Jax london 2011
Jax london 2011Jax london 2011
Jax london 2011
 
What's new in Java EE 6
What's new in Java EE 6What's new in Java EE 6
What's new in Java EE 6
 
gcdtmp
gcdtmpgcdtmp
gcdtmp
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Double checkedlockingjavasingletons
Double checkedlockingjavasingletonsDouble checkedlockingjavasingletons
Double checkedlockingjavasingletons
 
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
 
Eureka moment
Eureka momentEureka moment
Eureka moment
 

Similar to Concurrent Programming in Java

Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVMBernhard Huemer
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410huangachou
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10huangachou
 
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 (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Martijn Verburg
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programmingTausun Akhtary
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?Ramith Jayasinghe
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Multithreading in Scala
Multithreading in Scala Multithreading in Scala
Multithreading in Scala Knoldus Inc.
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK toolsHaribabu Nandyal Padmanaban
 
Ruby Concurrency
Ruby ConcurrencyRuby Concurrency
Ruby ConcurrencyEgor Hamaliy
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Sachintha Gunasena
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump AnalysisDmitry Buzdin
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuningosa_ora
 

Similar to Concurrent Programming in Java (20)

Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading in Scala
Multithreading in Scala Multithreading in Scala
Multithreading in Scala
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Ruby Concurrency
Ruby ConcurrencyRuby Concurrency
Ruby Concurrency
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
 
Java threading
Java threadingJava threading
Java threading
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupCatarinaPereira64715
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄCzechDreamin
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Julian Hyde
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomCzechDreamin
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 

Recently uploaded (20)

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ
10 Differences between Sales Cloud and CPQ, Blanka DoktorovĂĄ
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 

Concurrent Programming in Java

  • 2. 2 Agenda  United at birth  Why more relevant now  Concurrency over the years  Basics & Myths  Concurrency Patterns  Deep-dive  There's more than one way to solve it!!  Common gotchas  Q&A
  • 3. 3 Java & Concurrency : United at birth ● In-built into the language ● Not an after thought ● Support for threads from Day 1 ● Everything is a monitor ● Simplicity over complexity ● Multiple inheritance ● Operator overloading ● Choice of languages ● Scala, Clojure
  • 4. 4 Why is it more relevant now • H/W & S/W trends – No longer privilege of few – Scaling accentuates Vertical and horizontal scaling mean more complexity – Distributed caches, faster I/O – Weak Cache coherence • Application requirements – Impunity of slow system lifted – Elasticity is the mantra – The weakest link draws the whole system down – Bottlenecks can be really really costly
  • 5. 5 Concurrency over the years Java VersionJava Version FeaturesFeatures JDK 1.0 ●Threads ●“synchronized” keyword ●HashTable(thread-safe but synchronized classes) JDK 1.2 ●Collections framework(updated) ●un-synchronized classes bound by synchronized accessors ●eg: Collections.synchronizedMap() ●Use when you need sync JDK 1.5 ● concurrency package JSR166 java.util.concurrent JDK 7 ● Updated java.util.concurrent package(JSR166y) Fork-Join and Executor Framework JDK 8 ● Modularity ● Lamda
  • 6. 6 Concurrency : Basics Three elements of a concurrent Application  Atomicity  Certain pieces of an application must all be executed as one unit ● Visibility  Changes that you make to a value to be visible precisely when you intend them to be ● Synchronization  Concurrent access to shared variables should be guarded Terms you will come across ● JMM – The specification that guarantees how the JVM should work ● Monitor – Intrinsic lock (Every object has a monitor associated with it) ● Race condition – An operation on shared resources whose outcome may be in- deterministic ● 
  • 7. 7 Concurrency : Myths ● The more the threads, faster my application is • A bigger system means faster execution • Processors/compilers can optimize my code for parallelism • Locks make the program go slow • Re-factoring is easy • Writing concurrent application is a black-art
  • 8. 8 Common concurrency patterns Dynamic Exclusion Through Implicit Locks • a.k.a “synchronized” • Lock obtained if method is declared synchronized • Watch out for fine/coarse grained locks. • Dos/Dont's: – Avoid CPU/IO intensive operations inside “synchronized” block. – “synchronize” on a code block and not method (if appropriate) • Remember that JIT can re-order execution
  • 9. 9 Common concurrency patterns Structural Exclusion Through Confinement • Options: – Thread confinement : Access object from a single thread using thread-per-session approach – Instance confinement : Use encapsulation techniques to restrict access to object state from multiple threads – Method confinement : Do not allow an object to escape from method by referencing it through local variables • Do's/Dont's: – Confinement cannot be relied on unless a design is leak proof – Combine confinement with appropriate locking discipline – Use ThreadLocal variables to maintain Thread Confinement
  • 10. 10 Common concurrency patterns Immutability • Object state cannot change after construction • Are automatically thread-safe • Immutability is guranteed only when : – All fields are declared final – Object reference fields must not allow modifications anywhere in the object graph reachable from the fields after construction – The class should be declared final (to prevent a subclass from subverting these rules) • Immutable objects eliminate a bunch of issues around concurrency. Use whenever you can. • Have “failure atomicity” - upon an exception, state is never undesirable or indeterministic. "Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible." - Joshua Blochbe made immutable, limit its mutability as much as possible." - Joshua Bloch
  • 11. 11 Common concurrency patterns Co-operation • Wait-notify mechanisms • One thread creates a condition and another one waits for it • All threads are treated the same - when multiple threads are waiting, no guarantee on which one would wake up • Missed or early notifications • Do's/Dont's: – Use notifyAll() instead of notify() – Wait() in a while loop – Call wait() and notify() inside synchronized block
  • 12. 12 Deep Dive • JSR166 - “java.util.concurrent” packages • Introduced in JDK 5 • Motivation – Creating APIs for commonly used functionality – Application/scenario agnostic – “volatile” guaranteed visibility but not atomicity – Synchronization is expensive (and can cause dead-lock) ● Atomic Variables, nanosecond timing, Condition variables, Lock classes, tryLock, Queues, Barriers, Executors..... ●
  • 13. 13 Deep Dive – Atomic Classes • java.util.concurrent.atomic.Atomic* classes ● Provides a worthy substitute for “volatile” and “synchronized” ● Available on Integer, Long, Boolean, Object Ref, Arrays ● CompareAndSet(), incrementAndGet() etc guarantee atomicity
  • 14. 14 Deep Dive : Locks ReentrantLock  “synchronization” on steroids  Supports Lock state, non-blocking tryLock() and interruptible locking  Throughput can be higher by order of magnitudes  Can be extended to set “fairness” and “Condition”        Use ONLY and ONLY if “synchronized” will not solve your problems  If your Application needs timed-locks, interruptible locks, multiple condition locks
  • 15. 15 Deep Dive : Executor Framework ● Implemented via java.util.concurrent.ExecutorService ● Provides flexible thread pool implementation ● Producer-consumer pattern – Producer : Creates work – Consumer : Executes the work ● ● ● ● ● Consider newCachedThreadPool if you don't want to bound no. of threads
  • 16. 16 Deep Dive : Executor Framework ● Remember to shut it down ● Asynchronous execution may hide hidden tasks ●
  • 17. 17 Deep Dive: Fork Join Framework “Framework for supporting a style of programming in which problems are solved by recursively splitting them in to sub tasks that are solved in parallel, waiting for them to complete and then composing results” ● Introduced through JSR166y and part of Java 7 ● Introduces Parallel-Array (PA) concept to Java (simlar to Map-Reduce?) ● Extends the executor framework for Recursive style problems ● Makes use of work stealing algorithm ● Transparent Parallelism requiring no tuning (almost!) ● Suited for MMP Architectures. ● Number of sub tasks can be altered at run-time  “Fork” Starts a new parallel fork/join subtask  “Join” causes current task not to proceed until the forked task is complete  This can happen recursively till tasks are smaller enough to be solved sequentially Components of a FJ framework ● The guy who does the work (business logic) ● The guy who tells how the work can be split ● The guy who coordinates it all (FJ)
  • 18. 18 There's more than one way to solve it ● There's always more than one way to solve the problem ● Know the pros/cons of each approach ● Example : Not Thread Safe
  • 19. 19 Option 1 : Using “volatile” keyword Pros: ● Simple, easy to write and understand ● Would solve most of the requirement Cons: ● Thread looping ● No option to know/set priority of waiting threads There's more than one way to solve it
  • 20. 20 Option 2 : Using “AtomicBoolean” Pros: ● Fairly straight-forward ● “tryLock()” can prevent costly thread spins ● Flexibility to set Condition and “fairness” of waiting threads Cons: ● Slight over-head if you need primitive data locking There's more than one way to solve it
  • 21. 21 Option 3 : Using “synchronized” Pros: ● Locks are intrinsic and so no need to release them explicitly Cons ● Code is a lot more verbose ● Empty thread looping There's more than one way to solve it
  • 22. 22 Common concurrency mistakes ● synchronization – Fine/coarse grained synchronization – Synchronizing just data – Validating data integrity at one level – Synchronizing on “null” – Changing synchronizing object instance – Synchronizing on string literals – Writing with synchronization but reading w/o it – Volatile array - • “valarr” is a volatile reference to an array, but array elements are not
  • 23. 23 Common concurrency mistakes ● Volatile will work only when – write to variable does not depend on current value – Write does not depend on any other non-atomic operation ● CachedThreadPool is un-bound – You may run out of resources sooner than you thought ● ● The list never ends..... ● Recommend go through “Java Platform Concurrency Gotchas” - Alex Miller ●
  • 24. 24 Re-cap  Relevance of concurrency  Basics & Myths  Concurrency Patterns  Deep-dive  There's more than one way to solve it!!  Common gotchas  Q&A