Java read/write locks, which are provided by the official example reader, reentrantreadwritelock

Source: Internet
Author: User
The following example shows the built-in reentrantreadwritelock instance in the Java API concurrent library.
Class cacheddata {object data; volatile Boolean cachevalid; reentrantreadwritelock RWL = new reentrantreadwritelock (); void processcacheddata () {RWL. readlock (). Lock (); // @ 1 If (! Cachevalid) {// must release read lock before acquiring write lock RWL. readlock (). unlock (); // @ 4 RWL. writelock (). lock (); // @ 2 // recheck state because another thread might have acquired // write lock and changed state before we did. if (! Cachevalid) {// @ 3 data =... cachevalid = true;} // downgrade by acquiring read lock before releasing write lock RWL. readlock (). lock (); RWL. writelock (). unlock (); // unlock write, still hold read} Use (data); RWL. readlock (). unlock ();}}
 
// When n threads use the same cacheddata instance object to call the processcacheddata method, thread concurrency will occur.
 @ 1: When a thread is writing data, the thread running to @ 1 needs to wait for the write operation to complete, because the first thread running to @ 2 will add a lock and then require the data, during which no thread is allowed to read or write, 
after writing the data, add the read Lock operation on the thread to prevent other threads from writing data again after the write lock is uncompleted. it is a lot of multithreading after the first thread running to @ 2.
it may have been run to @ 4. After the data is modified, the write lock is removed, other threads will execute to @ 2. At this time, the first thread has been modified by data, so the @ 3 judgment is available.
when writing a multi-threaded Program , consider it in a concurrent thread environment and use reentrantreadwritelock skillfully, when using the lock, pay attention to the lock downgrade. The write lock can get the read lock, but the read lock cannot get the write lock. Therefore, when writing the lock, you must first remove the read lock and then read the lock.
Notes for use:
Read locks are write locks, read locks are not read locks, and multiple read locks can be read concurrently without blocking. That is, after the read lock is obtained and before the read lock is released, the write lock cannot be obtained by any thread.
During the simultaneous operation of multiple read locks, all threads trying to obtain the write lock are waiting, when the last read lock is released, the thread attempting to obtain the write lock has the opportunity to obtain the write lock.
write locks are operated by write locks and read locks. After a thread obtains the write lock, all other threads that attempt to obtain the write lock and read the lock are waiting until the write lock is released.
the write lock can obtain the read lock, namely:
RWL. writelock (). lock ();
// you can obtain the read lock in the write lock status.
RWL. readlock (). lock ();
RWL. writelock (). unlock ();
the read lock cannot be obtained. To add a write lock, the thread must release the read lock held by the lock, that is,
RWL. readlock (). lock ();
//......
// The read lock must be released before the write lock can be applied.
RWL. readlock (). unlock ();
RWL. writelock (). lock ();
Related Article

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.