Home> Java> javaTutorial> body text

What is the difference between Lock and Synchronized in Java

WBOY
Release: 2023-04-17 19:19:05
forward
1213 people have browsed it

1. From a functional point of view,

LockandSynchronizedare both tools used in Java to solve thread safety issues

2. From the perspective of characteristics,

Synchronizedis the synchronization keyword in java. Lock is the interface provided in the J.U.C package. This interface has many implementation classes, including important ones like ReentrantLock. To implement locking, Synchronized can control the strength of the lock in two ways

What is the difference between Lock and Synchronized in Java

One is to modify thesynchronizedkeyword at the method level, the other is to The first is to modify the code block. You can control the scope of the lock through the life cycle of the lock object. If the lock object is a static object or a class object, then the lock is a global lock. If The lock object is an ordinary instance object, so the scope of the lock depends on the life cycle of the instance. The granularity of the lock in Lock is determined by the lock() method andunlock()method provided in it

What is the difference between Lock and Synchronized in Java

As shown above:

The code between the lock() method and theunlock()method can ensure thread safety , and the scope of the lock depends on the life cycle of the Lock instance. Lock is more flexible thanSynchronized. Lock can decide independently when to lock and when to release the lock. You only need to call the lock() and unlock methods. Lock also provides A non-blocking competition lock method is calledtryLock(). This method can tell the current thread whether other threads are already using the lock by returning true/false, and Synchronized is a keyword, so it It is impossible to implement a non-blocking competitive lock method.SynchronizedThe release of the lock is passive. It will be released only after the execution of the Synchronized synchronized code block is completed, or when an exception occurs in the code. Lock provides a fair lock. And the mechanism of unfair lock. Fair lock means that when threads compete for lock resources, other threads are already queuing or waiting for the lock to be released. Then the thread currently competing for the lock cannot jump into the queue. Unfair lock means that no matter whether there is When a thread is queuing for a lock, it will try to compete for a lock. Synchronized only provides an unfair lock implementation.3. From a performance perspective,

Synchronized

is not much different from lock in terms of performance, but there will be some differences in implementation,SynchronizedIntroducing biased locks, lightweight locks, heavyweight locks and lock upgrade mechanisms to achieve lock optimization, while Lock uses spin locks to achieve performance optimization

The above is the detailed content of What is the difference between Lock and Synchronized in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!