Lock synchronized more object-oriented ways than the traditional threading model. Lock and life-like, should be an object. Two threads run on a piece of code to achieve a mutually exclusive effect of synchronization. They must use the same lock object.
Read -write Lock: Divided into reading lock and write lock. Multiple read locks are not mutually exclusive, and read and write locks are mutually exclusive, which is controlled by the JVM itself, and you only need to have the corresponding lock. Suppose your code reads only the data and can be read by very many people at the same time. But can't write at the same time, then read the lock. Suppose your code changes the data. There is only one person to write. and cannot be read at the same time, then write the lock. Anyway. Read the lock when reading, write a lock on the time of writing!
Reentrantreadwritelock will use two locks to solve this problem. A read lock. A write lock
Prerequisites for a thread to enter a read lock: There are no write locks for other threads, no write requests or write requests, but the calling thread and the thread holding the lock are the same.
Prerequisites for a thread to enter a write lock: No read lock for other threads. There are no write locks for other threads.
Summarize the characteristics of this locking mechanism:
(a). The internal writelock of the re-entry can acquire Readlock, But in turn Readlock want to get Writelock will never think.
(b). Writelock can be downgraded to readlock. The order is: Get Writelock and get readlock first. Then release Writelock, at which point the thread will remain readlock held. In turn Readlock want to upgrade to Writelock is not possible, why? See (a), hehe.
(c) . Readlock can be held by multiple threads and repel whatever writelock, while Writelock is completely mutually exclusive.
This feature is most important. Because of the relatively low write data structure for high read frequency. Using this type of lock-synchronization mechanism can increase the amount of concurrency.  
(d).
(e) . Writelock supports condition and is consistent with reentrantlock semantics. Readlock is not allowed to use condition, otherwise throws a Unsupportedoperationexception exception.
code example:
Package Cn.itcast.lesson12;import Java.io.objectoutputstream.putfield;import Java.util.random;import Java.util.concurrent.locks.reentrantlock;import Java.util.concurrent.locks.reentrantreadwritelock;import Org.omg.corba.public_member;public class Readwritelocktest {public static void main (string[] args) {final Queue3 q3 = new Queue3 (); for (int i=0, i < 3; i++) {new Thread () {public void run () {while (true) {Q3.get ()}}}. Start (); new Thread () {public void run () {while (true) {///pass in a data value q3.put (new Random (). Nextint (10000));}}. Start ();}}} Class Queue3{private Object data = null;//shared data, and only one thread can write it. But there are multiple threads that can read the data private reentrantreadwritelock RW1 = new Reentrantreadwritelock ();p ublic void Get () {Rw1.readlock (). Lock (); System.out.println (Thread.CurrentThread (). GetName () + "be ready to read data!"); Try{thread.sleep ((Long) (Math.random () *1000));} catch (Exception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "has read data:" + data); Rw1.readlock (). Unlock ();} Public void put (Object data) {Rw1.writelock (). Lock (); System.out.println (Thread.CurrentThread (). GetName () + "be ready to write data!"); Try{thread.sleep ((Long) (Math.random () *1000));} catch (Exception e) {e.printstacktrace ();} This.data = data; System.out.println (Thread.CurrentThread (). GetName () + "have write data:" + data); Rw1.writelock (). Unlock ();}}Results viewed:
Results Analysis: . Readlock can be held by multiple threads and repel whatever writelock, while Writelock is completely mutually exclusive. This feature is most important because of the relatively low write data structure for high read frequency. Using this type of lock-synchronization mechanism can increase the amount of concurrency.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Java Multithreading: Reentrantreadwritelock read-write lock use