Explicit Lock
1. Lock and ReentrantLock
Lock provides an unconditional, pollable, timed and interruptible lock acquisition operation. All locking operations and unlocking methods are explicit
ReentrantLock implements Lock: and provides the same memory semantics as synchronized; it also provides reentrant locking semantics
1. Basic semantics:
void LockDefinition() { Lock lock = new ReentrantLock();try {//do someting//更新对象状态,捕获异常;并在必要时恢复不变性条件} finally {//finally中释放锁 lock.unlock(); } }
2. Polling lock and timed lock
lock.tryLock([Long,TimeUnit]): Try to acquire the lock, and the time is a timed lock
3. Interruptible lock acquisition operation
lock.lockInterruptibly();
2. Fairness
ReentrantLock can create fair locks (requested sequential acquisition of locks) and unfair locks (queue-breaking).
Queue jumping: When a thread requests an unfair lock, if the status of the lock is available when the request is made, then the thread will not be put into the queue, but will skip all waiting threads in the queue and obtain the lock;
Note: Unfair locks do not advocate queue jumping, but they cannot prevent queue jumping; fair locks will be placed in the queue and executed sequentially
Unfair locks are faster than fair locks when competition is fierce Fast: The reason is that there is a serious delay between resuming a thread in the queue and the thread starting to run
3. Read-write lock
ReentrantLock is a standard mutex lock, but In some scenarios such as: parallel reading and reading cannot be achieved
ReadWriteLock read-write lock: implement ReentrantReadWriteLock: method readLock read lock and writeLock write lock
1. Interaction and implementation method
Release priority: When a write lock is released, and there are both reads and writes in the queue, should the read, write, or the thread that sends the request first be given priority?
Read thread jumps in the queue: There is a write lock waiting for the current read lock, so should the subsequent read lock jump in the queue and read directly? Direct reading will improve concurrency but may result in write starvation and failure to obtain
Reentrancy: Are read locks and write locks reentrant?
Demotion: Write lock, can you acquire a read lock without releasing the lock, so that the thread's lock can be downgraded?
Upgrade: Can read locks be upgraded to write locks before other waiting threads? If two threads try to upgrade to write locks at the same time, it is easy to cause a deadlock
Applicable to: data structures that mainly read operations
The above is the detailed content of Detailed explanation of explicit locks in java concurrent programming. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.