Java thread series --- read/write lock ReentrantReadWriteLock

Source: Internet
Author: User

Obtain order
This class does not impose priority on the reader or writer for the sorting of locked access. However, it does support optional fairness policies. When a thread is constructed fairly, the thread uses an approximate sequential strategy to compete for access. When the write lock is released, the write lock is assigned to a single writer with the longest wait time. If there is a reader with a longer wait time than all the writers, the read lock is assigned to the reader set. When a non-public ground constructs a thread, it does not need to be locked in the order of arrival. In either case, if the reader is active and a writer is locked, before obtaining the writer and releasing the write lock, the read lock will not be granted to any subsequent readers.

Reimport

This lock allows the reader and writer to re-obtain the read lock or write lock according to the ReentrantLock style. Only when all write locks maintained by the write thread are released can the writer use them.

In addition, the writer can obtain read locks-but in turn cannot. In other applications, it is useful to re-import data when the read operation is performed in the read lock mode during the call or callback. If the reader tries to get the write lock, it will never succeed.

Lock downgrade

Re-entry can also be downgraded from write lock to read lock. The implementation method is to first get the write lock, then get the read lock, and finally release the write lock. However, upgrading from read lock to write lock is impossible.

Monitoring

This class supports some methods to determine whether to keep the lock or to use the lock. These methods are designed to monitor the system status, rather than synchronous control.

The serialization method of such behaviors is the same as that of built-in locks: The deserialization locks are unlocked, regardless of the status of the locks when they are serialized.

Example usage:

The following code demonstrates the simulation of a simple cache system.


Public class CacheDemo {

Private Map <String, Object> cache = new HashMap <String, Object> ();
Public static void main (String [] args ){
// TODO Auto-generated method stub
}


Private ReadWriteLock rwl = new ReentrantReadWriteLock ();
Public Object getData (String key ){
Rwl. readLock (). lock (); // Add a read lock
Object value = null;
Try {
Value = cache. get (key );
If (value = null ){
Rwl. readLock (). unlock (); // release the read lock
Rwl. writeLock (). lock (); // Add a write lock
Try {
If (value = null ){
Value = "aaaa"; // query the queryDB database ();
}
} Finally {
Rwl. writeLock (). unlock ();
}
Rwl. readLock (). lock ();
}
} Finally {
Rwl. readLock (). unlock ();
}
Return value;
}
}

Author: wwww1988600

 

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.