Reading and writing locks: Divided into read locks and writing locks, multiple read locks do not reprimand, read locks and write locks mutually exclusive, which is controlled by JVM itself. You only need to apply the corresponding lock. If your code reads only the data, many people can read at the same time, but cannot write at the same time, then read the lock; if your code modify the data, you can only write a person and not read at the same time, then write the lock Essence In short, read the lock when reading, and write the lock when writing!
Three threads read data, three threads write data example:
You can read at the same time, you can't write it at the same time, you cannot write at the same time, and you cannot read it when writing.
Read the lock when you read, read the unlock; write the lock when you write, and the unlock.
Pay attention to finally unlock.
package com.ljq.test.thread; Import Java.util.random; Import Java.util.Current.Locks.ReadwriteLock; Import tardwritelock; /*** read and write lock** @Author administrator * */Public Class ReadWriteLockTest {Public Static Void Main (String [] ARGS) {Final Readwrite RW = New Readwrite (); for (Int I = 0; I <3; I ++) ew Thread () {Public Void Run ( ) {While (True) {rw.read ();}} .start (); new thread () {public void run () {space (true) {rw.write (new random (). NEXTINT (10000) );}}} .start ();}}} /** * read and write mutually exclusive, so put them in the same class * * @Authouse Administrator * * /Class Readwrite {Private Object Data = NULL ; // Share data, only one thread can write the data, but there are multiple threads to read the data at the same time. Readwritelock RWL = New ReentrantReadwritelock (); / *** Read data* / place void () {rwl.readLock (). Lock (); Renthread (). Getname () + "Be Ready to read data!"); Thread.sleep ((Long) (Math.random () * 1000)); System.out.println (Thread.CurrenThread (). GetName () + "Have Read Data:" " + data);} Catch (InterruptedException E) {e.printstacktrace ();} Finally {rwl.readlock (). Unlock ();} / ** * write data * * @param data * / publi c void write (Object data) {rwl.writelock (). Lock (); Try {system.out.println (thread.currentthread (). Getname () + "be ready to write data!"); H .random () * 1000)); this.data = data; system.out.println (thread.currentthRead (). GetName () + "Have Write Data:" + Data); tion e) {e. Printstacktrace ();} Finally {rwl.writelock (). Unlock ();}}}
Design a cache system cache system: If you need to take the data, you need to call my Public Object GetData (String Key) method. I want to check if I have this data inside Numbs, after finding this data into the memory inside me, someone will come to this data next time, and I will return this number directly without finding it in the database. Do not find the database for data, come to me.
package com.ljq.test.thread; Import Java.util.hashmap; Import Java.util.map; Import Java.util.Concurrent.Locks.Readwritelock; Import il.concurrent.locks.ReentrantRantreadwriteLock; /*** Design A cache system * * * @AutHor Administrator * */Public Class Cachedemo {Private Map <string, Object> Cache = New HashMap <string, Object> (); Public Static OID Main (String [] args) {string key = " name "; CacheDemo CacheDemo = New CacheDemo (); System.out.println (cachedemo.getdata (KEY)); ata (key)); // Data System.out.println (cachedemo.getdata (key)); // Get data from caching} Private ReadwriteLock RWL = New Reentrantreadwritelock (); Public Objectdata g key) {rwl.readlock (). LOCK (); / /Read the lock object value = null; try {value = cache.get (key); // First check if there is a value in the internal memory if (value == null) {// If not, check in the database to go to the database , And save the found results in the internal memory // Release the read lock, write lock rwl.readlock (). Unlock (); rwl.writelock (). Lock (); try {if (value == null) {// Judgment again to prevent multiple threads from being blocked in this place Repeat System.out.println ("Read Data from DataBase"); Value = "Zhang San"; Cache.put (key, value);}} finally {// Set the release of the release of the release lock rwl.writeLock (). Unlock ();} // Rw to restore the read and write status rwl.readlock (). lock (); ");}} Finally {rwl.readlock (). UnLock (); // Release the read lock} Return value;}}
Back results: