SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Lock Interface
Synchronized and limitation





Synchronized keyword doesn't support fairness

A thread can be blocked waiting for lock, for an indefinite
period of time and there was no way to control that.
Lock Interface
Lock interface provide opportunity of acquiring lock by different
ways:
* if we are using lock() and the lock is not available the current
thread will be suspended until the lock will be released;
* lockInterruptible() method acquires the lock until the
current thread will be interrupted or lock will be released;
continue...
* tryLock() method acquires the lock only if it is available at
the time of invocation (non-blocking, not waiting for the lock
will be released);
* If we want to acquire the lock interruptibly with the specified
waiting timeout we should use tryLock(long timeout,
TimeUnit timeUnit) method.
* There is only one method for unlocking the lock: unlock()
and it works as it named.
Main Differences Between Locks and
Synchronized Blocks
* A synchronized block makes no guarantees about the
sequence in which threads waiting to entering it are granted
access.


* You cannot pass any parameters to the entry of a
synchronized block. Thus, having a timeout trying to get
access to a synchronized block is not possible.




* The synchronized block must be fully contained within a
single method. A Lock can have it's calls to lock() and
unlock() in separate methods
What is ReentrantLock in Java
On class level, ReentrantLock is a concrete
implementation of Lock interface provided in Java
concurrency package from Java 1.5 onwards.
Thread can acquire the same lock multiple times without
any issue.
Reentrant locking increments special thread-personal
counter (unlocking - decrements) and the lock will be
released only when counter reaches zero.
ReentrantLock is mutual exclusive lock,
Two events are 'mutually exclusive' if they cannot occur at
the same time.
Benefits of ReentrantLock in Java

1) Ability to lock interruptibly.
2) Ability to timeout while waiting for lock.
3) Power to create fair lock.
4) API to get list of waiting thread for lock.
5) Flexibility to try for lock without blocking.

Weitere ähnliche Inhalte

Was ist angesagt? (6)

Scorex meetup-aug-2015
Scorex meetup-aug-2015Scorex meetup-aug-2015
Scorex meetup-aug-2015
 
Java thread
Java threadJava thread
Java thread
 
Thread
ThreadThread
Thread
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
2 con control
2 con control2 con control
2 con control
 

Andere mochten auch

Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
IndicThreads
 

Andere mochten auch (20)

Save JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOMSave JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOM
 
Java多线程技术
Java多线程技术Java多线程技术
Java多线程技术
 
Building a lock profiler on the JVM
Building a lock profiler on the JVMBuilding a lock profiler on the JVM
Building a lock profiler on the JVM
 
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
 
Git基础培训
Git基础培训Git基础培训
Git基础培训
 
App开发过程的演变之路
App开发过程的演变之路App开发过程的演变之路
App开发过程的演变之路
 
Nginx+tomcat https 配置
Nginx+tomcat  https 配置Nginx+tomcat  https 配置
Nginx+tomcat https 配置
 
如何更好地设计测试用例-BQConf
如何更好地设计测试用例-BQConf如何更好地设计测试用例-BQConf
如何更好地设计测试用例-BQConf
 
大型网站架构演变
大型网站架构演变大型网站架构演变
大型网站架构演变
 
Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧
 
浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage Collection
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
 
[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
 
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
On heap cache vs off-heap cache
On heap cache vs off-heap cacheOn heap cache vs off-heap cache
On heap cache vs off-heap cache
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission Control
 

Ähnlich wie Lock Interface in Java

Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Subhajit Sahu
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 

Ähnlich wie Lock Interface in Java (20)

Java reentrant lock tutorial by jeetendra mandal
Java reentrant lock tutorial by jeetendra mandalJava reentrant lock tutorial by jeetendra mandal
Java reentrant lock tutorial by jeetendra mandal
 
Locks
LocksLocks
Locks
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs
 
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...
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
 
Architectural patterns part 3
Architectural patterns part 3Architectural patterns part 3
Architectural patterns part 3
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Understanding Monitor in Dalvik
Understanding Monitor in DalvikUnderstanding Monitor in Dalvik
Understanding Monitor in Dalvik
 
Java threading
Java threadingJava threading
Java threading
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Lock Interface in Java

  • 2. Synchronized and limitation   Synchronized keyword doesn't support fairness A thread can be blocked waiting for lock, for an indefinite period of time and there was no way to control that.
  • 3. Lock Interface Lock interface provide opportunity of acquiring lock by different ways: * if we are using lock() and the lock is not available the current thread will be suspended until the lock will be released; * lockInterruptible() method acquires the lock until the current thread will be interrupted or lock will be released;
  • 4. continue... * tryLock() method acquires the lock only if it is available at the time of invocation (non-blocking, not waiting for the lock will be released); * If we want to acquire the lock interruptibly with the specified waiting timeout we should use tryLock(long timeout, TimeUnit timeUnit) method. * There is only one method for unlocking the lock: unlock() and it works as it named.
  • 5. Main Differences Between Locks and Synchronized Blocks * A synchronized block makes no guarantees about the sequence in which threads waiting to entering it are granted access.  * You cannot pass any parameters to the entry of a synchronized block. Thus, having a timeout trying to get access to a synchronized block is not possible.   * The synchronized block must be fully contained within a single method. A Lock can have it's calls to lock() and unlock() in separate methods
  • 6. What is ReentrantLock in Java On class level, ReentrantLock is a concrete implementation of Lock interface provided in Java concurrency package from Java 1.5 onwards. Thread can acquire the same lock multiple times without any issue. Reentrant locking increments special thread-personal counter (unlocking - decrements) and the lock will be released only when counter reaches zero. ReentrantLock is mutual exclusive lock, Two events are 'mutually exclusive' if they cannot occur at the same time.
  • 7. Benefits of ReentrantLock in Java 1) Ability to lock interruptibly. 2) Ability to timeout while waiting for lock. 3) Power to create fair lock. 4) API to get list of waiting thread for lock. 5) Flexibility to try for lock without blocking.