SlideShare a Scribd company logo
CONCURRENT PROGRAMMING
SYNCHRONIZATION (PART 1)
PROGRAMMAZIONE CONCORRENTE E DISTR.
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2015 – 2016
rcardin@math.unipd.it
Programmazione concorrente e distribuita
SUMMARY
 Introduction
 Thread-safety
 Race conditions
 Locking
 Locking pitfalls
2Riccardo Cardin
Programmazione concorrente e distribuita
INTRODUCTION
 Threads can reduce development cost and
improve the performance application
 Exploiting multiple processors
 Improved throughput by utilizing available processors
 Simplicity of modeling
 Use every thread to do a specific task is simplier than using
one thread to do them all
 Simplified handling of asynchronous events
 Prevention of server’s stall while it is fulfilling a request
 More reponsive user interfaces
 Use of different threads for GUI and event management
 Event dispatch thread (EDT)
3Riccardo Cardin
Programmazione concorrente e distribuita
INTRODUCTION
 Risks of threads
 Safety hazards
 Without sufficient synchronization, the ordering of
operations in multiple threads is unpredictable
4Riccardo Cardin
public class UnsafeSequence {
private int value;
/** Returns a unique value. */
public int getNext() {
return value++; // Three operations: read, add and store
}
}
With unlucky timing, two
threads could call
getNext and receive the
same value
Programmazione concorrente e distribuita
INTRODUCTION
 Risks of threads
 Liveness hazards
 An activity gets into a state such that it is permanently unable
to make forward progress
 Dining philosophers problem
 Performance hazards
 Poor service time, responsiveness, throughput,
resource consumption, or scalability
 Context switch is not cost free
5Riccardo Cardin
If thread A is waiting for a resource that thread B holds
exclusively, and B never releases it, A will wait forever.
Programmazione concorrente e distribuita
INTRODUCTION
6Riccardo Cardin
Programmazione concorrente e distribuita
THREAD SAFETY
 Writing thread-safe code is about managing
access to shared and mutable state
 Object state is represented by its data
 By Shared, we mean that a variable could be
accessed by multiple threads
 By mutable, that its value could change
 It is far easier to design a class to be thread-safe than
to retrofit it later
7Riccardo Cardin
Whenever more than a thread accesses a given state variable, and one
of them might write to it, they all must coordinate their access to it
using synchronization
-- Brian Goetz
Programmazione concorrente e distribuita
THREAD SAFETY
 There are three ways to fix a mutable shared
variable
 Don’t share the state variable across threads
 Make the state variable immutable
 Use synchronization whenever accessing it
 Object oriented techniques favor thread safety
 Encapsulation
 Immutability
 Clear specification of invariants
8Riccardo Cardin
Stateless objects are always thread-safe.
-- Brian Goetz
Programmazione concorrente e distribuita
THREAD SAFETY
9Riccardo Cardin
Programmazione concorrente e distribuita
RACE CONDITIONS
 Race conditions
 The correctness of a computation depends on
relative timing of multiple threads at runtime
 Atomicity
 A set of statements is not atomic if they are not executed in a
single, indivisible operation
 Read-modifiy-write
 Check-then-act
10Riccardo Cardin
A class is thread-safe if it behaves correctly when accessed from
multiple threads, regardless of the scheduling or interleaving of the
execution of those threads by the runtime environment.
-- Brian Goetz
Programmazione concorrente e distribuita
RACE CONDITIONS
 Read-modify-write race condition
 The value of value is read, then modified adding 1
and finally stored into value variable
 Among the execution of every statement, control flow could
be preempted by another thread
11Riccardo Cardin
public class UnsafeSequence {
private int value;
/** Returns a unique value. */
public int getNext() {
value = value + 1;
return value;
}
}
read value value + 1 store value
Possible preemption
Programmazione concorrente e distribuita
RACE CONDITIONS
 Check-then-act race condition
 Lazy initialization
 The boolean expression depends on a value that is
modified according to it
12Riccardo Cardin
public class LazyInitRace {
private ExpensiveObject instance = null;
public ExpensiveObject getInstance() {
// Check-then-act
if (instance == null)
instance = new ExpensiveObject();
return instance;
}
}
check instance create object store instance
Possible preemption
Programmazione concorrente e distribuita
RACE CONDITIONS
 Compound actions
 Sequences of operations that must be executed
atomically in order to remain thread-safe.
 Read-modify-write and Check-then-act must always be
atomic to be thread-safe
 Atomicity is relative to operation that are executed
on shared state
 The java.util.concurrent.atomic package contains
atomic variable classes for effecting atomic state transitions
13Riccardo Cardin
public class SafeSequence {
private AtomicInteger value;
public int getNext() {
return value.incrementAndGet(); // Atomic read-modify-write
}
}
Programmazione concorrente e distribuita
RACE CONDITIONS
14Riccardo Cardin
Programmazione concorrente e distribuita
SHARING STATE
 Writing correct concurrent programs is primarily
about managing access to shared, mutable state
 It’s all about memory visibility
 We want to ensure that when a thread modifies the state of
an object, other threads can actually see those changes
 If shared state is represented by more than one variable,
atomic classes are not useful
15Riccardo Cardin
public class UnsafeCachedSequence {
private AtomicInteger lastValue;
private AtomicInteger value;
public int getNext() {
// Invariant of the class is not satisfied anymore
lastValue.set(value.get());
return value.incrementAndGet();
}
}
Programmazione concorrente e distribuita
LOCKING
 To preserve state consistency, update related
state variable in a single atomic operation
 Java has a built-in locking mechanism for enforcing
atomicity: synchronized block
 But, it is easier to understand the synchronized
keyword after having seen locks in isolation...
16Riccardo Cardin
synchronized (lock) {
// Access or modify shared state guarded by lock
}
Object that will serve
as lock
Block code to be guarded by
the lock
Programmazione concorrente e distribuita
LOCKING
 Reentrant locking
 Use a Lock to protect a code block
 The construct guarantees that only one thread at time
can enter the critical section
 Always release the lock in a finally block to prevents deadlocks
 The class ReentranctLock implements basic functionalities of
a lock
17Riccardo Cardin
myLock.lock(); // a ReentrantLock object
try {
// Operation in this block are executed atomically
} finally {
// make sure the lock is unlocked even if an
// exceptions thrown
myLock.unlock();
}
Programmazione concorrente e distribuita
LOCKING
 Reentrant locking
 The lock acts as mutual exclusion locks
18Riccardo Cardin
public class SafeCachedSequence {
private Lock lock = new ReentrantLock();
private int lastValue;
private int value;
public int getNext() {
// Invariant of the class is now satisfied
lock.lock();
try {
lastValue = value;
value = value + 1;
int result = value;
} finally {
lock.unlock();
}
return result;
}
}
Now the invariant of
the class is
guaranteed by the
lock: value and
lastValue will always
be updated in a
consistent way
Programmazione concorrente e distribuita
LOCKING
 Reentrant locking
19Riccardo Cardin
Thread 1 Thread 2
getNext getNext
Unsynchronized
Thread 1 Thread 2
getNext
getNext
Synchronized
Programmazione concorrente e distribuita
LOCKING
 Reentrant locking
 Different threads have to synchronize using the same
instance of the lock
 It is called reetrant because a thread can repeatedly
acquire a lock that it already owns
 The lock has a hold count that keeps track of the nested calls
to the lock method
 Prevents deadlocks wrt the subclass mechanism
 Every object in Java (since 1.0) has an intrinsic lock
 The synchronized keyword on a method protects the
access to that method using this reference as lock object
 All method’s code is guarded
20Riccardo Cardin
Programmazione concorrente e distribuita
LOCKING
 Intrinsic locking
 To call the method, a thread must acquire the
intrinsic object lock
 Static synchronized methods use the Class object as lock.
21Riccardo Cardin
public synchronized void method() {
// method body
}
// ...is equivalent to
public void method() {
this.intrinsicLock.lock();
try {
// method body
} finally {
this.intrinsicLock.unlock();
}
}
Programmazione concorrente e distribuita
LOCKING
 Intrinsic locking suffers of performance issues
 Use synchronization by Lock object
 Or synchronized block
 It uses the reference to an object that will serve as lock
22Riccardo Cardin
public class SafeCachedSequence {
private Object lock = new Object();
private int lastValue;
private int value;
public int getNext() {
synchronized (lock) {
lastValue = value;
value = value + 1;
int result = value;
}
return result;
}
}
Programmazione concorrente e distribuita
LOCKING
23Riccardo Cardin
Programmazione concorrente e distribuita
LOCKING PITFALLS
 All the accesses to a mutable shared variable
must be performed with the same lock held
 Not only compound actions
 Not all data needs to be guarded by a lock
 Only mutable data
 All the variables involved in the same invariant
must be guarded by the same lock
 It is not sufficient to use intrinsic lock on every
method
24Riccardo Cardin
// Not thread-safe
if (!vector.contains(element))
vector.add(element);
Programmazione concorrente e distribuita
LOCKING PITFALLS
 Poor concurrency
 Limits by the availability of processing resources, not
by the structure of application itself
 CPU intensive and I/O operations must be outside
synchronized blocks
 Acquiring and releasing a lock has some overhead
 Not break down synchronized blocks too far
25Riccardo Cardin
There is frequently a tension between simplicity and performance.
When implementing a synchronization policy, resist the temptation to
prematurely sacrifice simplicity (potentially compromising safety) for
the sake of performance.
-- Brian Goetz
Programmazione concorrente e distribuita
EXAMPLES
26Riccardo Cardin
https://github.com/rcardin/pcd-snippets
Programmazione concorrente e distribuita
REFERENCES
 Chap. 1 «Introduction», Java Concurrency in Practice, Brian Goetz,
2006, Addison-Wesley Professional
 Chap. 2 «Thread Safety», Java Concurrency in Practice, Brian Goetz,
2006, Addison-Wesley Professional
 Chap. 3 «Sharing Objects», Java Concurrency in Practice, Brian
Goetz, 2006, Addison-Wesley Professional
 Dining philosophers problem
https://en.wikipedia.org/wiki/Dining_philosophers_problem
 Chap. 14 «Multithreading», Core Java Volume I - Fundamentals, Cay
Horstmann, Gary Cornell, 2012, Prentice Hall
 Intrinsic Locks and Synchronization
https://docs.oracle.com/javase/tutorial/essential/concurrency/lock
sync.html
27Riccardo Cardin

More Related Content

What's hot (20)

PPTX
Java - Processing input and output
Riccardo Cardin
 
PPT
Unit I Advanced Java Programming Course
parveen837153
 
PDF
Managing Binary Compatibility in Scala (Scala Days 2011)
mircodotta
 
PPT
Introduction to-vhdl
Neeraj Gupta
 
PDF
Advanced Reflection in Java
kim.mens
 
PPS
Java session13
Niit Care
 
PDF
Java Faqs useful for freshers and experienced
yearninginjava
 
PDF
Detecting aspect-specific code smells using Ekeko for AspectJ
Coen De Roover
 
ZIP
J3d hibernate
KRANTHICMR
 
PPTX
Java - Sockets
Riccardo Cardin
 
ODP
Method Handles in Java
hendersk
 
PDF
Lecture1
karim_ibrahim
 
PDF
Ekeko Technology Showdown at SoTeSoLa 2012
Coen De Roover
 
PDF
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
Coen De Roover
 
PDF
Javainterview
Amarjit03
 
PDF
Java Concurrency Quick Guide
Anton Shchastnyi
 
PDF
System Verilog Functional Coverage
rraimi
 
PDF
JavaFX In Practice
Richard Bair
 
PPT
Strategy and Template Pattern
Jonathan Simon
 
Java - Processing input and output
Riccardo Cardin
 
Unit I Advanced Java Programming Course
parveen837153
 
Managing Binary Compatibility in Scala (Scala Days 2011)
mircodotta
 
Introduction to-vhdl
Neeraj Gupta
 
Advanced Reflection in Java
kim.mens
 
Java session13
Niit Care
 
Java Faqs useful for freshers and experienced
yearninginjava
 
Detecting aspect-specific code smells using Ekeko for AspectJ
Coen De Roover
 
J3d hibernate
KRANTHICMR
 
Java - Sockets
Riccardo Cardin
 
Method Handles in Java
hendersk
 
Lecture1
karim_ibrahim
 
Ekeko Technology Showdown at SoTeSoLa 2012
Coen De Roover
 
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
Coen De Roover
 
Javainterview
Amarjit03
 
Java Concurrency Quick Guide
Anton Shchastnyi
 
System Verilog Functional Coverage
rraimi
 
JavaFX In Practice
Richard Bair
 
Strategy and Template Pattern
Jonathan Simon
 

Viewers also liked (14)

PPTX
Java Graphics Programming
Riccardo Cardin
 
PPTX
Design Pattern Architetturali - Dependency Injection
Riccardo Cardin
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPTX
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
PPTX
Design Pattern Strutturali
Riccardo Cardin
 
PPTX
Design pattern architetturali Model View Controller, MVP e MVVM
Riccardo Cardin
 
PPTX
Diagrammi di Sequenza
Riccardo Cardin
 
PPTX
Errori comuni nei documenti di Analisi dei Requisiti
Riccardo Cardin
 
PDF
Presto updates to 0.178
Kai Sasaki
 
PPTX
Introduzione ai Design Pattern
Riccardo Cardin
 
PPTX
Diagrammi delle Classi
Riccardo Cardin
 
PPTX
Software architecture patterns
Riccardo Cardin
 
KEY
Scala For Java Programmers
Enno Runne
 
PPTX
SOLID - Principles of Object Oriented Design
Riccardo Cardin
 
Java Graphics Programming
Riccardo Cardin
 
Design Pattern Architetturali - Dependency Injection
Riccardo Cardin
 
Java - Collections framework
Riccardo Cardin
 
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
Design Pattern Strutturali
Riccardo Cardin
 
Design pattern architetturali Model View Controller, MVP e MVVM
Riccardo Cardin
 
Diagrammi di Sequenza
Riccardo Cardin
 
Errori comuni nei documenti di Analisi dei Requisiti
Riccardo Cardin
 
Presto updates to 0.178
Kai Sasaki
 
Introduzione ai Design Pattern
Riccardo Cardin
 
Diagrammi delle Classi
Riccardo Cardin
 
Software architecture patterns
Riccardo Cardin
 
Scala For Java Programmers
Enno Runne
 
SOLID - Principles of Object Oriented Design
Riccardo Cardin
 
Ad

Similar to Java- Concurrent programming - Synchronization (part 1) (20)

PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
 
PPTX
Concurrency
Ankur Maheshwari
 
PPTX
Concurrency
Sandeep Chawla
 
PDF
Java concurrency in practice
Deon Huang
 
PPTX
Thread & concurrancy
Onkar Deshpande
 
PDF
Concurrency: Best Practices
IndicThreads
 
PDF
Concurrency in Java
Lakshmi Narasimhan
 
PDF
Concurrent Programming in Java
Lakshmi Narasimhan
 
DOC
Concurrency Learning From Jdk Source
Kaniska Mandal
 
PPT
4 Multithreading and Parallel Programming.ppt
MahmoudGad93
 
DOCX
Java 5 concurrency
priyank09
 
PPTX
Computer architecture related concepts, process
ssusera979f41
 
PPTX
Low-level concurrency (reinvent vehicle)
Olena Syrota
 
PDF
Concurrency on the JVM
Bernhard Huemer
 
PPTX
concurrency control
rajshreemuthiah
 
ODP
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
PPT
Reliable and Concurrent Software - Java language
ssuser2637a1
 
PPTX
The Java Memory Model
CA Technologies
 
PDF
Java Tutorials - Concurrency
Christian Rubiales
 
PPTX
Synchronization problem with threads
Syed Zaid Irshad
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
 
Concurrency
Ankur Maheshwari
 
Concurrency
Sandeep Chawla
 
Java concurrency in practice
Deon Huang
 
Thread & concurrancy
Onkar Deshpande
 
Concurrency: Best Practices
IndicThreads
 
Concurrency in Java
Lakshmi Narasimhan
 
Concurrent Programming in Java
Lakshmi Narasimhan
 
Concurrency Learning From Jdk Source
Kaniska Mandal
 
4 Multithreading and Parallel Programming.ppt
MahmoudGad93
 
Java 5 concurrency
priyank09
 
Computer architecture related concepts, process
ssusera979f41
 
Low-level concurrency (reinvent vehicle)
Olena Syrota
 
Concurrency on the JVM
Bernhard Huemer
 
concurrency control
rajshreemuthiah
 
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
Reliable and Concurrent Software - Java language
ssuser2637a1
 
The Java Memory Model
CA Technologies
 
Java Tutorials - Concurrency
Christian Rubiales
 
Synchronization problem with threads
Syed Zaid Irshad
 
Ad

More from Riccardo Cardin (7)

PPTX
Design Pattern Comportamentali
Riccardo Cardin
 
PPTX
Design Pattern Creazionali
Riccardo Cardin
 
PPTX
Diagrammi di Attività
Riccardo Cardin
 
PPTX
Diagrammi Use Case
Riccardo Cardin
 
PPTX
Introduzione a UML
Riccardo Cardin
 
PPTX
Mvc e di spring e angular js
Riccardo Cardin
 
PPTX
Reactive programming principles
Riccardo Cardin
 
Design Pattern Comportamentali
Riccardo Cardin
 
Design Pattern Creazionali
Riccardo Cardin
 
Diagrammi di Attività
Riccardo Cardin
 
Diagrammi Use Case
Riccardo Cardin
 
Introduzione a UML
Riccardo Cardin
 
Mvc e di spring e angular js
Riccardo Cardin
 
Reactive programming principles
Riccardo Cardin
 

Recently uploaded (20)

PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
For my supp to finally picking supp that work
necas19388
 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 

Java- Concurrent programming - Synchronization (part 1)

  • 1. CONCURRENT PROGRAMMING SYNCHRONIZATION (PART 1) PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 [email protected]
  • 2. Programmazione concorrente e distribuita SUMMARY  Introduction  Thread-safety  Race conditions  Locking  Locking pitfalls 2Riccardo Cardin
  • 3. Programmazione concorrente e distribuita INTRODUCTION  Threads can reduce development cost and improve the performance application  Exploiting multiple processors  Improved throughput by utilizing available processors  Simplicity of modeling  Use every thread to do a specific task is simplier than using one thread to do them all  Simplified handling of asynchronous events  Prevention of server’s stall while it is fulfilling a request  More reponsive user interfaces  Use of different threads for GUI and event management  Event dispatch thread (EDT) 3Riccardo Cardin
  • 4. Programmazione concorrente e distribuita INTRODUCTION  Risks of threads  Safety hazards  Without sufficient synchronization, the ordering of operations in multiple threads is unpredictable 4Riccardo Cardin public class UnsafeSequence { private int value; /** Returns a unique value. */ public int getNext() { return value++; // Three operations: read, add and store } } With unlucky timing, two threads could call getNext and receive the same value
  • 5. Programmazione concorrente e distribuita INTRODUCTION  Risks of threads  Liveness hazards  An activity gets into a state such that it is permanently unable to make forward progress  Dining philosophers problem  Performance hazards  Poor service time, responsiveness, throughput, resource consumption, or scalability  Context switch is not cost free 5Riccardo Cardin If thread A is waiting for a resource that thread B holds exclusively, and B never releases it, A will wait forever.
  • 6. Programmazione concorrente e distribuita INTRODUCTION 6Riccardo Cardin
  • 7. Programmazione concorrente e distribuita THREAD SAFETY  Writing thread-safe code is about managing access to shared and mutable state  Object state is represented by its data  By Shared, we mean that a variable could be accessed by multiple threads  By mutable, that its value could change  It is far easier to design a class to be thread-safe than to retrofit it later 7Riccardo Cardin Whenever more than a thread accesses a given state variable, and one of them might write to it, they all must coordinate their access to it using synchronization -- Brian Goetz
  • 8. Programmazione concorrente e distribuita THREAD SAFETY  There are three ways to fix a mutable shared variable  Don’t share the state variable across threads  Make the state variable immutable  Use synchronization whenever accessing it  Object oriented techniques favor thread safety  Encapsulation  Immutability  Clear specification of invariants 8Riccardo Cardin Stateless objects are always thread-safe. -- Brian Goetz
  • 9. Programmazione concorrente e distribuita THREAD SAFETY 9Riccardo Cardin
  • 10. Programmazione concorrente e distribuita RACE CONDITIONS  Race conditions  The correctness of a computation depends on relative timing of multiple threads at runtime  Atomicity  A set of statements is not atomic if they are not executed in a single, indivisible operation  Read-modifiy-write  Check-then-act 10Riccardo Cardin A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment. -- Brian Goetz
  • 11. Programmazione concorrente e distribuita RACE CONDITIONS  Read-modify-write race condition  The value of value is read, then modified adding 1 and finally stored into value variable  Among the execution of every statement, control flow could be preempted by another thread 11Riccardo Cardin public class UnsafeSequence { private int value; /** Returns a unique value. */ public int getNext() { value = value + 1; return value; } } read value value + 1 store value Possible preemption
  • 12. Programmazione concorrente e distribuita RACE CONDITIONS  Check-then-act race condition  Lazy initialization  The boolean expression depends on a value that is modified according to it 12Riccardo Cardin public class LazyInitRace { private ExpensiveObject instance = null; public ExpensiveObject getInstance() { // Check-then-act if (instance == null) instance = new ExpensiveObject(); return instance; } } check instance create object store instance Possible preemption
  • 13. Programmazione concorrente e distribuita RACE CONDITIONS  Compound actions  Sequences of operations that must be executed atomically in order to remain thread-safe.  Read-modify-write and Check-then-act must always be atomic to be thread-safe  Atomicity is relative to operation that are executed on shared state  The java.util.concurrent.atomic package contains atomic variable classes for effecting atomic state transitions 13Riccardo Cardin public class SafeSequence { private AtomicInteger value; public int getNext() { return value.incrementAndGet(); // Atomic read-modify-write } }
  • 14. Programmazione concorrente e distribuita RACE CONDITIONS 14Riccardo Cardin
  • 15. Programmazione concorrente e distribuita SHARING STATE  Writing correct concurrent programs is primarily about managing access to shared, mutable state  It’s all about memory visibility  We want to ensure that when a thread modifies the state of an object, other threads can actually see those changes  If shared state is represented by more than one variable, atomic classes are not useful 15Riccardo Cardin public class UnsafeCachedSequence { private AtomicInteger lastValue; private AtomicInteger value; public int getNext() { // Invariant of the class is not satisfied anymore lastValue.set(value.get()); return value.incrementAndGet(); } }
  • 16. Programmazione concorrente e distribuita LOCKING  To preserve state consistency, update related state variable in a single atomic operation  Java has a built-in locking mechanism for enforcing atomicity: synchronized block  But, it is easier to understand the synchronized keyword after having seen locks in isolation... 16Riccardo Cardin synchronized (lock) { // Access or modify shared state guarded by lock } Object that will serve as lock Block code to be guarded by the lock
  • 17. Programmazione concorrente e distribuita LOCKING  Reentrant locking  Use a Lock to protect a code block  The construct guarantees that only one thread at time can enter the critical section  Always release the lock in a finally block to prevents deadlocks  The class ReentranctLock implements basic functionalities of a lock 17Riccardo Cardin myLock.lock(); // a ReentrantLock object try { // Operation in this block are executed atomically } finally { // make sure the lock is unlocked even if an // exceptions thrown myLock.unlock(); }
  • 18. Programmazione concorrente e distribuita LOCKING  Reentrant locking  The lock acts as mutual exclusion locks 18Riccardo Cardin public class SafeCachedSequence { private Lock lock = new ReentrantLock(); private int lastValue; private int value; public int getNext() { // Invariant of the class is now satisfied lock.lock(); try { lastValue = value; value = value + 1; int result = value; } finally { lock.unlock(); } return result; } } Now the invariant of the class is guaranteed by the lock: value and lastValue will always be updated in a consistent way
  • 19. Programmazione concorrente e distribuita LOCKING  Reentrant locking 19Riccardo Cardin Thread 1 Thread 2 getNext getNext Unsynchronized Thread 1 Thread 2 getNext getNext Synchronized
  • 20. Programmazione concorrente e distribuita LOCKING  Reentrant locking  Different threads have to synchronize using the same instance of the lock  It is called reetrant because a thread can repeatedly acquire a lock that it already owns  The lock has a hold count that keeps track of the nested calls to the lock method  Prevents deadlocks wrt the subclass mechanism  Every object in Java (since 1.0) has an intrinsic lock  The synchronized keyword on a method protects the access to that method using this reference as lock object  All method’s code is guarded 20Riccardo Cardin
  • 21. Programmazione concorrente e distribuita LOCKING  Intrinsic locking  To call the method, a thread must acquire the intrinsic object lock  Static synchronized methods use the Class object as lock. 21Riccardo Cardin public synchronized void method() { // method body } // ...is equivalent to public void method() { this.intrinsicLock.lock(); try { // method body } finally { this.intrinsicLock.unlock(); } }
  • 22. Programmazione concorrente e distribuita LOCKING  Intrinsic locking suffers of performance issues  Use synchronization by Lock object  Or synchronized block  It uses the reference to an object that will serve as lock 22Riccardo Cardin public class SafeCachedSequence { private Object lock = new Object(); private int lastValue; private int value; public int getNext() { synchronized (lock) { lastValue = value; value = value + 1; int result = value; } return result; } }
  • 23. Programmazione concorrente e distribuita LOCKING 23Riccardo Cardin
  • 24. Programmazione concorrente e distribuita LOCKING PITFALLS  All the accesses to a mutable shared variable must be performed with the same lock held  Not only compound actions  Not all data needs to be guarded by a lock  Only mutable data  All the variables involved in the same invariant must be guarded by the same lock  It is not sufficient to use intrinsic lock on every method 24Riccardo Cardin // Not thread-safe if (!vector.contains(element)) vector.add(element);
  • 25. Programmazione concorrente e distribuita LOCKING PITFALLS  Poor concurrency  Limits by the availability of processing resources, not by the structure of application itself  CPU intensive and I/O operations must be outside synchronized blocks  Acquiring and releasing a lock has some overhead  Not break down synchronized blocks too far 25Riccardo Cardin There is frequently a tension between simplicity and performance. When implementing a synchronization policy, resist the temptation to prematurely sacrifice simplicity (potentially compromising safety) for the sake of performance. -- Brian Goetz
  • 26. Programmazione concorrente e distribuita EXAMPLES 26Riccardo Cardin https://github.com/rcardin/pcd-snippets
  • 27. Programmazione concorrente e distribuita REFERENCES  Chap. 1 «Introduction», Java Concurrency in Practice, Brian Goetz, 2006, Addison-Wesley Professional  Chap. 2 «Thread Safety», Java Concurrency in Practice, Brian Goetz, 2006, Addison-Wesley Professional  Chap. 3 «Sharing Objects», Java Concurrency in Practice, Brian Goetz, 2006, Addison-Wesley Professional  Dining philosophers problem https://en.wikipedia.org/wiki/Dining_philosophers_problem  Chap. 14 «Multithreading», Core Java Volume I - Fundamentals, Cay Horstmann, Gary Cornell, 2012, Prentice Hall  Intrinsic Locks and Synchronization https://docs.oracle.com/javase/tutorial/essential/concurrency/lock sync.html 27Riccardo Cardin