Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Lock and Synchronized in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the difference between Lock and Synchronized in Java". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between Lock and Synchronized in Java".

1. From a functional point of view

Lock and Synchronized are both tools used in java to solve thread safety problems.

two。 In terms of characteristics,

Synchronized is the synchronization keyword in java, Lock is the interface provided in the J.U.C package, and this interface has many implementation classes, including the implementation of reentering locks such as ReentrantLock. Synchronized can control the strength of the lock in two ways.

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

As shown in the figure above: the code between the lock () method and the unlock () method ensures thread safety, and the scope of the lock depends on the life cycle of the Lock instance. Lock is more flexible than Synchronized. Lock can decide when to lock and release the lock by calling lock () and unlock. Lock also provides a non-blocking competitive lock method, called tryLock (), which can tell the current thread whether another thread is already using the lock by returning true/false, and Synchronized is a keyword. Therefore, it cannot implement the method of non-blocking competitive lock. The release of Synchroized lock is passive, and it will only be released when Synchronized synchronizes the code block and ends execution, or when the code is abnormal. Lock provides a mechanism for fair lock and unfair lock. Fair lock means that when threads compete for lock resources, other threads are already queuing or waiting for lock release, so threads currently competing for locks cannot jump the queue. An unfair lock means that it will try to compete for a lock regardless of whether there are threads queuing for the lock or not. Synchronized only provides an implementation of the unfair lock.

3. From a performance point of view

There is little difference in performance between Synchronized and lock, and there will be some differences in implementation. Synchronized introduces mechanisms of bias lock, lightweight lock, heavyweight lock and lock upgrade to optimize lock, while spin lock is used in Lock to achieve performance optimization.

Thank you for your reading, the above is the content of "what is the difference between Lock and Synchronized in Java". After the study of this article, I believe you have a deeper understanding of what is the difference between Lock and Synchronized in Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report