Lock is more object-oriented than the synchronized approach in the traditional threading model, similar to a lock in life, and the lock itself should be an object. Code fragments executed by two threads to achieve the effect of a synchronous mutex, they must use the same lock object.
Read and write Lock: divided into reading lock and write lock, multiple read lock is not mutually exclusive, read lock and write lock mutex, this is controlled by the JVM itself, you only need to good the corresponding lock. If your code is read-only and can be read by a lot of people at the same time, but not at the same time, read the lock; If your code modifies the data, only one person is writing and cannot read at the same time, write the lock. In short, read the time to read the lock, write the time to write the lock!
Reentrantreadwritelock will use two locks to solve the problem, a read lock, a write lock
Prerequisites for the thread to enter the read lock:
No other thread has a write lock,
There are 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
No other thread's write lock
To Reentrantreadwritelock, the first thing to do is to draw a distinction with reentrantlock. It and the latter are separate implementations, with no inheritance or implementation relationships. Then we summarize the characteristics of this locking mechanism:
(a). The writelock of its internal can get readlock, but in turn Readlock want to get Writelock never think about it.
(b). Writelock can be downgraded to readlock, in order to get Writelock to get readlock and then release Writelock, when the thread will hold readlock. 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 any writelock while in action, while Writelock is completely mutually exclusive. This feature is most important because the use of such a lock synchronization mechanism can increase concurrency for data structures with relatively low writes for high read frequencies.
(d). Both Readlock and Writelock support interrupt, and semantics are consistent with Reentrantlock.
(e). Writelock supports condition and is consistent with reentrantlock semantics, while Readlock does not use condition, or throws unsupportedoperationexception exceptions.
Here is an example of a read-write lock:
Package Com.thread;import Java.util.random;import Java.util.concurrent.locks.reentrantreadwritelock;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 (); } for (int i=0;i<3;i++) {new Thread () {public void run () { while (true) {Q3.put (New Random (). Nextint (10000)); }}}.start (); }}class queue3{Private Object data = null;//shared data, only one thread can write the data, but multiple threads can read the data at the same time. Private Reentrantreadwritelock RWL = new Reentrantreadwritelock (); public void Get () {Rwl.readlock (). Lock ();//on Read lock, other threads can only read and cannot write System.out.println (Thread.CurrentThread (). GetName () + "be ready to read data!"); try {thread.sleep ((long) (Math.random () *1000)); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () + "has read data:" + data); Rwl.readlock (). Unlock (); Release the read lock, preferably placed inside the finnaly} public void put (Object data) {Rwl.writelock (). Lock ();//write lock, do not allow other threads to read or write Sy Stem.out.println (Thread.CurrentThread (). GetName () + "be ready to write data!"); try {thread.sleep ((long) (Math.random () *1000)); } catch (Interruptedexception e) {e.printstacktrace (); } this.data = data; System.out.println (Thread.CurrentThread (). GetName () + "have write data:" + data); Rwl.writelock (). unlock ();//Release Write Lock}}
Thread-0 is ready to read data! Thread-1 is ready to read data! Thread-2 is ready to read data! Thread-0have Read Data:nullthread-2have read Data:nullthread-1have read data:nullthread-5 be ready to write data! Thread-5 has write data:6934thread-5 is ready to write data! Thread-5 has write data:8987thread-5 is ready to write data! Thread-5 have write data:8496
The following uses a read-write lock to emulate a cache:
Package Com.thread;import Java.util.hashmap;import Java.util.map;import java.util.concurrent.locks.ReadWriteLock; Import Java.util.concurrent.locks.reentrantreadwritelock;public class Cachedemo {private map<string, Object> ma p = new hashmap<string, object> ();//cache private Readwritelock RWL = new Reentrantreadwritelock (); public static void Main (string[] args) {} public Object get (String ID) {Object value = NULL; Rwl.readlock (). Lock ();//First Open the read lock, from the cache to fetch try{value = map.get (ID); if (value = = null) {//If no read lock is released in the cache, write lock Rwl.readlock (). Unlock (); Rwl.writelock (). Lock (); try{if (value = = null) {value = "AAA"; At this point you can go to the database to find, here a simple simulation of}}finally{Rwl.writelock (). unlock ();//Release Write lock } rwl.readlock (). Lock (); And then read the lock}}finally{ Rwl.readlock (). Unlock (); Finally release the read lock} return value; }}
Use of Reentrantreadwritelock read-write locks in Java