Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between ReentrantLock and Synchronized

Shulou Source: shulou.com Published: 2022-06-02 02:31:11 09月17日 Update

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.

Tags: Threads complex manual not used right to use scenarios methods mechanisms times procedures reconciliation help different fashion longest clear not enough code advantages content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple NVidia Microsoft Shulou Information