Fair lock/unfair lock
Fair lock means that multiple threads acquire the lock in the order in which they apply for it Lock.
Unfair lock means that the order in which multiple threads acquire locks is not in the order in which they apply for locks. It is possible that the thread that applies later acquires the lock before the thread that applies first. It is possible that it will cause priority inversion or starvation.
For Java ReentrantLock, specify whether the lock is a fair lock through the constructor, and the default is an unfair lock. The advantage of unfair locks is that the throughput is greater than fair locks.
For Synchronized, it is also an unfair lock. Since it does not implement thread scheduling through AQS like ReentrantLock, there is no way to turn it into a fair lock.
Reentrant lock
Reentrant lock, also known as recursive lock, means that when the same thread acquires the lock in the outer method, it enters the inner method The lock will be acquired automatically. It's a bit abstract, but there is a code example below.
For Java ReentrantLock, its name can be seen as a reentrant lock, and its name is Re entrant Lock.
For Synchronized, it is also a reentrant lock. One benefit of reentrant locks is that deadlocks can be avoided to a certain extent.
synchronized void setA() throws Exception{ Thread.sleep(1000); setB(); } synchronized void setB() throws Exception{ Thread.sleep(1000); }
The above code is a feature of a reentrant lock. If it is not a reentrant lock, setB may not be executed by the current thread, which may cause a deadlock.
Exclusive lock/shared lock
Exclusive lock means that the lock can only be held by one thread at a time.
Shared lock means that the lock can be held by multiple threads.
For Java ReentrantLock, it is an exclusive lock. But for another implementation class of Lock, ReadWriteLock, its read lock is a shared lock and its write lock is an exclusive lock.
The shared lock of the read lock can ensure that concurrent reading is very efficient, and the processes of reading, writing, writing, and writing are mutually exclusive.
Exclusive locks and shared locks are also implemented through AQS. Exclusive or shared locks are implemented through different methods.
For Synchronized, of course it is an exclusive lock.
Mutual Exclusion Lock/Read-Write Lock
The exclusive lock/shared lock mentioned above is a broad term, and the mutex lock/read-write lock is specific realization.
The specific implementation of mutex lock in Java is ReentrantLock.
The specific implementation of read-write lock in Java is ReadWriteLock.
Optimistic Lock/Pessimistic Lock
Optimistic lock and pessimistic lock do not refer to specific types of locks, but to the perspective of viewing concurrency and synchronization.
Pessimistic lock believes that concurrent operations on the same data will definitely be modified. Even if there is no modification, it will be considered modified. Therefore, for concurrent operations on the same data, pessimistic locking takes the form of locking. Pessimistically, I believe that concurrent operations without locking will definitely cause problems.
Optimistic locking believes that concurrent operations on the same data will not be modified. When updating data, the data will be updated by trying to update and constantly re-updating the data. Optimistically, there will be no problem with concurrent operations without locking.
We can see from the above description that pessimistic locking is suitable for scenarios with a lot of write operations, and optimistic locking is suitable for scenarios with a lot of read operations. Not locking will bring a lot of performance improvements.
The use of pessimistic locking in Java is to use various locks.
The use of optimistic locking in Java is lock-free programming, and the CAS algorithm is often used. A typical example is the atomic class, which implements the update of atomic operations through CAS spin.
Segmented lock
Segmented lock is actually a lock design, not a specific lock. For ConcurrentHashMap, its concurrency implementation is Efficient concurrent operations are achieved through segmented locks.
Let’s take ConcurrentHashMap to talk about the meaning and design ideas of segment lock. The segment lock in ConcurrentHashMap is called Segment, which is similar to the structure of HashMap (the implementation of HashMap in JDK7 and JDK8), that is, the internal There is an Entry array, and each element in the array is a linked list; it is also a ReentrantLock (Segment inherits ReentrantLock).
When it is necessary to put an element, it does not lock the entire hashmap, but first uses the hashcode to know which segment it should be placed in, and then locks this segment, so when When multi-threaded put, as long as it is not placed in a segment, true parallel insertion is achieved.
However, when counting size, when obtaining the global information of hashmap, you need to obtain all segment locks to count.
The design purpose of segmented lock is to refine the granularity of the lock. When the operation does not need to update the entire array, only one item in the array is locked.
Bias lock/Lightweight lock/Heavyweight lock
These three types of locks refer to the status of the lock and are for Synchronized. In Java 5, efficient Synchronized is achieved by introducing a lock upgrade mechanism. The status of these three locks is indicated by the fields in the object header of the object monitor.
Biased lock means that a piece of synchronization code is always accessed by one thread, then the thread will automatically acquire the lock. Reduce the cost of acquiring locks.
Lightweight lock means that when the lock is a biased lock and is accessed by another thread, the biased lock will be upgraded to a lightweight lock. Other threads will try to acquire the lock through spin without blocking. , improve performance.
Heavyweight lock means that when the lock is a lightweight lock, although another thread is spinning, the spin will not continue forever. When it spins a certain number of times, it has not been acquired yet. lock, it will enter blocking, and the lock will expand into a heavyweight lock. Heavyweight locks will block other application threads and reduce performance.
Spin lock
In Java, a spin lock means that the thread trying to acquire the lock will not block immediately, but will use a loop to try to acquire the lock. , the advantage of this is to reduce the consumption of thread context switching, but the disadvantage is that the loop consumes CPU.
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of What are the differences between java locks?. 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的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

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

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
