The difference between Reentrantlock and synchronized

Source: Internet
Author: User

一、ReentrantLock类

ReentrantLockClass is implemented Lock , it has the synchronized same concurrency and memory semantics, but adds some features like lock polling , timed lock waiting , and interruptible lock waiting . In addition, it provides better performance in the case of intense contention (in other words, when many threads want to access shared resources, the JVM can spend less time scheduling threads and spending more on the execution thread)

What does the reentrant lock mean? Simply put, it has a lock-related get counter, if one of the threads that owns the lock gets the lock again, then the fetch counter increases by 1, and then the lock needs to be freed two times to get a true release. This mimics synchronized the semantics; If a thread enters a synchronized block protected by a monitor that the thread already owns, it allows the thread to proceed, and when the thread exits the second (or subsequent) synchronized block, does not release the lock, only the thread exits it enters the first of the monitor protection synchronizedblock, the lock is released.

Two, the performance difference between synchronized and Reentrantlock

The synchronized primitive uses the CPU pessimistic locking mechanism , that is, the thread obtains the exclusive lock . An exclusive lock means that other threads can only rely on blocking to wait for the thread to release the lock. When the CPU conversion thread is blocked, it causes the thread context switch, and when there are many threads competing for the lock, the CPU's frequent context switches are inefficient.

Lock uses an optimistic locking method . The so-called optimistic lock is that each time without locking but assuming that there is no conflict to complete an operation, if the conflict failed to retry until successful. The mechanism for optimistic locking is the CAS operation (Compare and Swap). We can further study the source code of Reentrantlock, and we will find that one of the more important ways to get a lock is compareandsetstate. This is actually the special instruction provided by the CPU that is invoked.

Three, synchronized and lock use difference

Synchronized primitives and Reentrantlock do not differ in general, but in very complex synchronization applications, consider using Reentrantlock, especially when you encounter the following 2 requirements.
1. A thread needs to be interrupted while waiting for control of a lock
2. You need to separate some wait-notify,reentrantlock inside the condition application, to control which thread notify
3. With the fair lock function, each incoming thread will be queued
The following thin way to ...
First of all, the Reentrantlock lock mechanism has 2 types, ignoring interrupt lock and response interrupt lock, which gives us a lot of flexibility. For example: If a, B2 thread to compete lock, a thread get lock, b thread wait, but a thread this time really have too many things to deal with, is not return, B thread may be able to wait, want to interrupt themselves, no longer wait for this lock, to deal with other things. This time Reentrantlock provides 2 mechanisms, first, the B thread interrupts itself (or another thread interrupts it), but Reentrantlock does not respond, continue to let the B thread wait, how you interrupt, I am all one ear (synchronized the original language is so) Second, the B thread interrupts itself (or the other thread interrupts it), Reentrantlock handles the interrupt, and no longer waits for the lock to come and completely abandon it.  

The difference between Reentrantlock and synchronized

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.