Java Concurrency Programming: concurrent lock mechanism analysis

Source: Internet
Author: User

Tag:perl   home    Read    blog   shared   write  gray    read lock    family    technology sharing    

Java Concurrency Programming: Concurrent lock mechanism parsing table of Contents
    • 1. Lock
    • 2. Reentrantlock
    • 3. Readwritelock
    • 4. Reentrantreadwritelock

Earlier, we talked about the object lock mechanism that comes with Java. Because our method must be in an object, the invocation of the method can be well controlled by the lock of the object. When the lock of an object is held by a thread, and other threads want to invoke that method of the object, they must go into the wait pool and wait until the current thread finishes executing, and the system decides which of the following will continue to execute. This method is very intuitive and the principle is very clear.
So why would Doug Lea develop an extra parallel package?
First, let's take a look at his lock lock and do the benefits.
I think the main benefits are:

    • Lock can query more information, including the currently held threads, the number of queued threads, and so on, which is very important, greatly improving the scope of application, which is the basis of a lot of concurrency classes later;
    • The separation of Read and write locks is equivalent to the addition of a shared lock on the basis of the original exclusive lock. For methods that do not require synchronization, a shared lock is used, and all threads can be called at the same time, synchronizing only external methods, which can greatly improve performance.
1Lock

The lock in the concurrent package is just an interface class and is not implemented by itself. It defines three main methods, lock (), Unlock (), newcondition (). Lock () is used for thread acquisition locks, when executed to this method, if the lock is not occupied by the thread, the lock is assigned to the thread, if it has been allocated, then wait; unlock () is used to unlock the thread; Newcondition () is used to create the condition. There are three other ways in which a thread acquires a lock, such as whether it can be interrupted after acquisition, to acquire locks in a heuristic way, and so on.

 Public Interface Lock {    void Lock();void lockinterruptibly() throws interruptedexception;Boolean Trylock();Boolean Trylock(Long  Time,Timeunit Unit) throws interruptedexception;void Unlock();Condition newcondition();}
2Reentrantlock

Reentrantlock is an implementation of the Lock interface class, which is essentially an exclusive lock. Use a state to save the number of times a thread calls lock (). When state is 0 o'clock, the lock can be held by the thread, then the state is changed to 1, so that the other thread cannot get the lock again, only that thread can hold again, which is the origin of the name of the lock. When the thread calls unlock (), the state value is reduced by 1, until it is again equal to 0, indicating that the thread has completely freed the lock.
This state amount is saved with an int, and the value exceeds the maximum positive integer represented by int, the overflow becomes negative, and the error is less than 0. Therefore, the same lock can re-enter the maximum number of integer.max_value times, that is, 2147483647.

int NEXTC = c + acquires; if (nextc < 0)    Throw New Error ("Maximum lock count Exceeded");

As for the underlying implementation, if you look at the source code, you will find that basically the CAS-based implementation is compare and swap. is to have a value that, when compared to whether the current and expected values are equal, updates the values when they are equal. Therefore, when multiple threads change a value at the same time, it is certain that only one thread can succeed. Because the expectations of these threads must be the same, when one of the threads modifies the value, the expectations of the other threads are not successful. Therefore, a maximum of one thread can execute successfully at a time.

3Readwritelock

Readwritelock is also an interface class with two methods that return a read lock and a write lock, but they share a single Aqs queue.


Lock Readlock (); Lock Writelock ();
4Reentrantreadwritelock

The separation of Read and write locks is very significant, because most of the time, most of our operations are reading data, and only a few cases require writing data, and if you use synchronous or re-entry locks directly, then the performance and efficiency will be very low.
Read and write locks have an essential difference, read locks are shared locks, and write locks are exclusive locks.

    • Read operations do not need to be synchronized, only when the write operation in progress requires synchronous wait, so when there is no write operation, is an empty lock, all threads can be called at the same time.
    • Write operations must be synchronized, so only one thread at a time can occupy a write lock.
    • Read and write locks cannot be held at the same time, whether a single thread or a different thread. When a read lock is read, the write lock cannot be held and must wait for all read operations to complete before acquiring a write lock. Similarly, when a write lock is held, the read lock cannot be held.

In fact, read and write locks are shared with a aqs queue, and state volume is also shared. The low 16 bits represent the write lock, and the high 16 bits represent the read lock. As a result, the maximum number of reentrant locks and read locks is 65,535.
The difference is that locks are acquired in different ways:

the way a lock is acquired by a read lock is to acquire a shared lock  Public void Lock (){    sync.acquireshared(1);}  Write lock acquires the lock in the same way that it acquires the exclusive lock publicvoidlock(){    sync.acquire(1); }

date:2017-07-08 10:22

Author:wen YANG

created:2017-07-09 Sun 23:16

Emacs 25.2.1 (ORG mode 8.2.10)

Validate

Java Concurrency Programming: concurrent lock mechanism analysis

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.