SlideShare a Scribd company logo
LOCKS
THINH DANG AN
VU TRAN QUANG
THREAD-SAFE
• In multi-threaded programming, threads share resources.
• It may lead to race condition.
• A piece of code is thread-safe if it functions correctly during simultaneous
execution by multiple threads.
• Synchronization is the most widely used method to achieve thread-safe.
• Lock is a kind of synchronization.
REENTRANTLOCK
• A lock is acquired via lock() and released via unlock().
• If another thread has already acquired the lock, subsequent calls to lock() pause
the current thread until the lock has been unlocked.
• Only one thread can hold the lock at any given time.
• tryLock() is an alternative to lock().
READWRITELOCK
• This type of lock maintains a pair of locks for read and write access.
• Read-lock can be held simultaneously by multiple threads as long as no threads
hold the write-lock.
• Improve performance and throughput in situations that reads are more frequent
than writes.
READWRITELOCK
STAMPEDLOCK
• Also support read and write locks.
• locking methods return a stamp.
• These stamps are used to release a lock, to check if the lock is still valid or to
convert the mode.
• Main features of StampedLock compared to ReadWriteLock is mode conversion
and optimistic read.
STAMPEDLOCK
MODE CONVERSION
When and how to convert mode:
• You acquired the write lock and wrote something and you want to read in the
same critical section, use tryConvertToReadLock(long stamp).
• You acquired the read lock, and after a successful read, you wanted to change the
value, use tryConvertToWriteLock(long stamp).
STAMPEDLOCK
OPTIMISTIC READ
StampedLock has 3 modes of access: Read, Write, Optimistic Read.
• Read, Write: equivalent to ReadWriteLock while returning a stamp.
• Optimistic Read: doesn't prevent another thread from getting a write lock, which
will make the optimistic read lock stamp's invalid.
SPINLOCK
• A lock which causes a thread trying to acquire it to simply wait in a loop ("spin")
while repeatedly checking if the lock is available.
• Efficient if threads are likely to be blocked for only short periods.
SPINLOCK
SEMAPHORE
• Locks usually grant exclusive access to variables or resources.
• Semaphores are capable of maintaining whole sets of permits.
• Useful in scenarios where you have to limit the amount concurrent access to
certain parts of your application.
DISTRIBUTED LOCK
• Common problems : how to ensure that only one
process across a fleet of servers acts on a resource:
• processes may pause for arbitrary lengths of time
• packets may be arbitrarily delayed in the network
• clocks may be arbitrarily wrong
DISTRIBUTED LOCK
DISTRIBUTED LOCK
• Safety and Liveness guarantees
1. Safety property: Mutual exclusion. At any given moment, only one client can
hold a lock.
2. Liveness property A: Deadlock free. Eventually it is always possible to acquire
a lock, even if the client that locked a resource crashes or gets partitioned.
3. Liveness property B: Fault tolerance. As long as the majority of Redis nodes
are up, clients are able to acquire and release locks.
REDLOCK
• In order to acquire the lock, the client performs the following
operations:
• 1. Get the current time.
• 2. … All the steps needed to acquire the lock …
• 3. Get the current time, again.
• 4. Check if we are already out of time, or if we acquired the lock fast enough.
• 5. Do some work with your lock.
REDLOCK

More Related Content

Similar to Locks

READER/WRITER SOLUTION
READER/WRITER SOLUTIONREADER/WRITER SOLUTION
READER/WRITER SOLUTION
Shahzeb Pirzada
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
Yu-Shuan Hsieh
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
Lakshmi Narasimhan
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
Lakshmi Narasimhan
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Operating system 27 semaphores
Operating system 27 semaphoresOperating system 27 semaphores
Operating system 27 semaphores
Vaibhav Khanna
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
Home
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
Allan Huang
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Bob Pusateri
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
Dmitry Buzdin
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
SivaRamaSundar Devasubramaniam
 
Concurrency
ConcurrencyConcurrency
Concurrency
Ankur Maheshwari
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Luis Goldster
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Hoang Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
James Wong
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Fraboni Ec
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Harry Potter
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Young Alista
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Tony Nguyen
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Bob Pusateri
 

Similar to Locks (20)

READER/WRITER SOLUTION
READER/WRITER SOLUTIONREADER/WRITER SOLUTION
READER/WRITER SOLUTION
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Java threading
Java threadingJava threading
Java threading
 
Operating system 27 semaphores
Operating system 27 semaphoresOperating system 27 semaphores
Operating system 27 semaphores
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
 

Recently uploaded

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 

Recently uploaded (20)

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 

Locks

  • 2. THREAD-SAFE • In multi-threaded programming, threads share resources. • It may lead to race condition. • A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads. • Synchronization is the most widely used method to achieve thread-safe. • Lock is a kind of synchronization.
  • 3. REENTRANTLOCK • A lock is acquired via lock() and released via unlock(). • If another thread has already acquired the lock, subsequent calls to lock() pause the current thread until the lock has been unlocked. • Only one thread can hold the lock at any given time. • tryLock() is an alternative to lock().
  • 4. READWRITELOCK • This type of lock maintains a pair of locks for read and write access. • Read-lock can be held simultaneously by multiple threads as long as no threads hold the write-lock. • Improve performance and throughput in situations that reads are more frequent than writes.
  • 6. STAMPEDLOCK • Also support read and write locks. • locking methods return a stamp. • These stamps are used to release a lock, to check if the lock is still valid or to convert the mode. • Main features of StampedLock compared to ReadWriteLock is mode conversion and optimistic read.
  • 7. STAMPEDLOCK MODE CONVERSION When and how to convert mode: • You acquired the write lock and wrote something and you want to read in the same critical section, use tryConvertToReadLock(long stamp). • You acquired the read lock, and after a successful read, you wanted to change the value, use tryConvertToWriteLock(long stamp).
  • 8. STAMPEDLOCK OPTIMISTIC READ StampedLock has 3 modes of access: Read, Write, Optimistic Read. • Read, Write: equivalent to ReadWriteLock while returning a stamp. • Optimistic Read: doesn't prevent another thread from getting a write lock, which will make the optimistic read lock stamp's invalid.
  • 9. SPINLOCK • A lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. • Efficient if threads are likely to be blocked for only short periods.
  • 11. SEMAPHORE • Locks usually grant exclusive access to variables or resources. • Semaphores are capable of maintaining whole sets of permits. • Useful in scenarios where you have to limit the amount concurrent access to certain parts of your application.
  • 12. DISTRIBUTED LOCK • Common problems : how to ensure that only one process across a fleet of servers acts on a resource: • processes may pause for arbitrary lengths of time • packets may be arbitrarily delayed in the network • clocks may be arbitrarily wrong
  • 14. DISTRIBUTED LOCK • Safety and Liveness guarantees 1. Safety property: Mutual exclusion. At any given moment, only one client can hold a lock. 2. Liveness property A: Deadlock free. Eventually it is always possible to acquire a lock, even if the client that locked a resource crashes or gets partitioned. 3. Liveness property B: Fault tolerance. As long as the majority of Redis nodes are up, clients are able to acquire and release locks.
  • 15. REDLOCK • In order to acquire the lock, the client performs the following operations: • 1. Get the current time. • 2. … All the steps needed to acquire the lock … • 3. Get the current time, again. • 4. Check if we are already out of time, or if we acquired the lock fast enough. • 5. Do some work with your lock.

Editor's Notes

  1. - Race condition: show example - Thread-safe: In particular, it must satisfy the need for multiple threads to access the same shared data, and the need for a shared piece of data to be accessed by only one thread at any given time.
  2. - Trylock: it tries to acquire the lock without pausing the current thread. - Reentrant means that locks are bound to the current thread. A thread can safely acquire the same lock multiple times without running into deadlocks.
  3. - The idea behind read-write locks is that it's usually safe to read mutable variables concurrently as long as nobody is writing to this variable.
  4. - The idea behind read-write locks is that it's usually safe to read mutable variables concurrently as long as nobody is writing to this variable.
  5. - ReadWriteLock is good, but it has some severe issues with starvations if not handled properly. For example, a number of reads but very few writes can cause the writer thread to fall into starvation. - Note: StampedLock is not reentrant, so each call to acquire the lock always returns a new stamp and blocks if there's no lock available, even if the same thread already holds a lock, which may lead to deadlock.
  6. - Note: Converting methods do not block and may return the stamp as zero, which means these methods' calls were not successful.
  7. - If using optimistic read, always check if the stamp is valid using the lock.validate(stamp) before reading.
  8. - Those instructions usually are much faster than synchronizing via locks. - Thread will access data from memory address and compare the current value with current expected value. If they match It will update the variable to new value. Obviously If multiple threads try making this operation simultaneously only one thread will succeed and others will fail. But other threads will not block instead invoker can continue with other operation or try the same again with failing threads. So this provides a lot better performance . 
  9. - A kind of busy waiting. - Because they avoid overhead from operating system process rescheduling or context switching.
  10. - A kind of busy waiting. - Because they avoid overhead from operating system process rescheduling or context switching.