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 ReentrantLock and Synchronized

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between ReentrantLock and Synchronized, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Public class ReentrantLock implements Lock, java.io.Serializable {}

The difference between ReentrantLock and Synchronized:

Both ReentrantLock and synchronized are exclusive locks that only allow threads with mutually exclusive access to the critical section. But the implementation of the two are different: the process of synchronized locking and unlocking is implicit, users do not need to operate manually, the advantage is that the operation is simple, but it is not flexible enough. It is enough to use synchronized in general concurrent scenarios; ReentrantLock needs to be locked and unlocked manually, and the unlocking operation should be placed in the finally code block as far as possible to ensure that the thread releases the lock correctly. ReentrantLock operations are complex, but because you can manually control the locking and unlocking process, it can be useful in complex concurrency scenarios.

Both ReentrantLock and synchronized are reentrant. Because synchronized is reentrant, it can be placed on the method that is recursively executed without worrying about whether the thread can release the lock correctly in the end, while ReentrantLock ensures that the lock must be acquired repeatedly as many times as the lock is released repeatedly, otherwise other threads may not be able to acquire the lock.

To put it simply: the operation of ReentrantLock is a little more complicated, but it can be locked and unlocked manually, and the operation is more flexible.

Specific differences:

1. Fair locks: unfair locks are used by default, and ReentrantLock can achieve fair locks (ReentrantLock lock = new ReentrantLock (true)). Description: fair lock means that when the lock is available, the thread with the longest waiting time on the lock will get the right to use the lock. On the other hand, the unfair lock randomly allocates this right of use.

2. Corresponding interrupt: ReentrantLock provides us with a lock acquisition method lockInterruptibly () that can respond to the interrupt.

3. Time-limited waiting: tryLock ()

4. Waiting for notification (wake-up) mechanism: lock.newCondition (); condition.await (); condition.signal (); queue blocking mechanism can be implemented

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report