Compared with the lock implementation in Java locks in Java, read/write locks are more complex. Assume that your program involves read and write operations on some shared resources, and write operations are less frequent than read operations. When no write operation is performedThere is no problem when two threads read a resource at the same time. Therefore, multiple threads should be allowed to read shared resources at the same time. However, if a thread wants to write these shared resources, there should be no other threads reading or writing this resource.(Note: Read-read can coexist, read-write cannot coexist, and write-write cannot coexist ). This requires a read/write lock to solve this problem. Java 5 contains the read/write lock in the Java. util. Concurrent package. Even so, we should understand the principles behind its implementation.
For read and write operations, you can use wait policy to ensure the security of common global variables.
Next, let's write a read/write lock:
Package COM. toov5.readandwritelock; import Java. util. hashmap; import Java. util. map; // The read/write lock is similar to the JVM built-in cache (store the value in JVM) public class writeandreadlock {private volatile Map <object, Object> cache = new hashmap <> (); // write element public void put (string key, string value) {try {thread. sleep (1, 500); system. out. println ("write" + key + value); cache. put (Key, value); system. out. println ("write" + key + value + "end");} catch (interruptedexception e) {e. printstacktrace () ;}/// read the Public String get (string key) {try {thread. sleep (1, 500); system. out. println ("read" + key); string result = cache. get (key ). tostring (); system. out. println ("read" + key + "end"); return result;} catch (exception e) {return NULL;} public static void main (string [] ARGs) {writeandreadlock lock = new writeandreadlock (); // write thread T1 = new thread (New runnable () {@ override public void run () {for (INT I = 0; I <10; I ++) {lock. put ("I", I + "") ;}}); // read thread t2 = new thread (New runnable () {@ override public void run () {for (INT I = 0; I <10; I ++) {lock. get (I + "") ;}}); t1.start (); t2.start ();}}
Result:
If synchronize is added to both methods, the data cannot be read when being read or written.
The read/write locks are provided below:
Package COM. toov5.readandwritelock; import Java. util. hashmap; import Java. util. map; import Java. util. concurrent. locks. reentrantreadwritelock; import Java. util. concurrent. locks. reentrantreadwritelock. readlock; import Java. util. concurrent. locks. reentrantreadwritelock. writelock; // The read/write lock is similar to the JVM built-in cache (store the value in JVM) public class writeandreadlock {private volatile Map <object, Object> cache = new hashmap <> (); private reentrantreadwritelock WRL = new reentrantreadwritelock (); Private writelock wirtelock = WRL. writelock (); // get the write lock private readlock = WRL. readlock (); // get the read lock // write element public void put (string key, string value) {try {wirtelock. lock (); thread. sleep (1, 500); system. out. println ("write" + key + value); cache. put (Key, value); system. out. println ("write" + key + value + "end");} catch (interruptedexception e) {e. printstacktrace ();} finally {wirtelock. unlock () ;}/// read the Public String get (string key) {try {readlock. lock (); thread. sleep (1, 500); system. out. println ("read" + key); string result = cache. get (key ). tostring (); system. out. println ("read" + key + "end"); return result;} catch (exception e) {return NULL;} finally {readlock. unlock () ;}} public static void main (string [] ARGs) {writeandreadlock lock = new writeandreadlock (); // write thread T1 = new thread (New runnable () {@ override public void run () {for (INT I = 0; I <10; I ++) {lock. put ("I", I + "") ;}}); // read thread t2 = new thread (New runnable () {@ override public void run () {for (INT I = 0; I <10; I ++) {lock. get (I + "") ;}}); t1.start (); t2.start ();}}
It cannot be read when it is not written.
View Source Code:
Unlike the previous lock, the previous lock is reentrantlock, which has two locks: read and write.
It is impossible for two threads to write at the same time and read at the same time. Not coexistence!
Reading, reading, and writing cannot coexist.
Read/write lock