Read and Write Lock: Reading lock and write lock, multiple read lock is not mutually exclusive, read lock and write lock mutually exclusive, this is controlled by the JVM itself, you just have to do the appropriate lock. If your code reads only data, many people can read at the same time, but not at the same time, then read the lock; If your code modifies the data, only one person is writing and cannot read at the same time, then write the lock. In short, read the lock when reading, write the lock on the writing time!
Three threads read data, three threads write Data sample:
Can be read at the same time, reading can not write, can not write at the same time, writing can not read.
Read the lock, read the unlock, write the lock, write the unlock.
Note finally unlock.
Package com.ljq.test.thread;
Import Java.util.Random;
Import Java.util.concurrent.locks.ReadWriteLock;
Import Java.util.concurrent.locks.ReentrantReadWriteLock;
/** * 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++) {new Thread () {public void run () {while (true) {RW.
Read ();
}}.start ();
New Thread () {public void run () {while (true) {Rw.write (New Random (). Nextint (10000));
}}.start (); }}/** * Read and write to be mutually exclusive, so put them in the same class * * @author Administrator * */class ReadWrite {private Object data = null;
Shared data, only one thread can write the data, but multiple threads may read the data at the same time.
Readwritelock rwl = new Reentrantreadwritelock ();
/** * Read Data */public void Read () {Rwl.readlock (). Lock (); try {System.out.println (Thread.CurrentThread (). GetName () + "be ready to read data!");
Thread.Sleep ((Long) (Math.random () * 1000));
System.out.println (Thread.CurrentThread (). GetName () + "have read data:" + data);
catch (Interruptedexception e) {e.printstacktrace ();
finally {Rwl.readlock (). Unlock (); }/** * Write Data * * @param/public void write (Object data) {Rwl.writelock (). Lock
();
try {System.out.println (Thread.CurrentThread (). GetName () + "be ready to write data!");
Thread.Sleep ((Long) (Math.random () * 1000));
This.data = data;
System.out.println (Thread.CurrentThread (). GetName () + "have write data:" + data);
catch (Interruptedexception e) {e.printstacktrace ();
finally {Rwl.writelock (). Unlock ();
}
}
}
Design a caching system
Caching system: You want to fetch the data, you need to call my public Object getData (String key) method, I want to check if I have this data inside, if there is a direct return, if not, look up this number from the database, After the data is stored in my internal memory, the next time someone to ask for this data, I will return to this number directly do not have to look in the database. You need to fetch the data not to look for the database, come to me.
Package com.ljq.test.thread;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.locks.ReadWriteLock;
Import Java.util.concurrent.locks.ReentrantReadWriteLock; /** * Design a caching system * * * @author Administrator */public class Cachedemo {private map<string, object> cache
= new hashmap<string, object> ();
public static void Main (string[] args) {String key = ' name ';
Cachedemo Cachedemo = new Cachedemo (); System.out.println (Cachedemo.getdata (key)); Obtain data from the database System.out.println (Cachedemo.getdata (key)); Gets the data from the cache System.out.println (Cachedemo.getdata (key));
Get data from cache} private Readwritelock rwl = new Reentrantreadwritelock ();
The public Object getData (String key) {Rwl.readlock (). Lock ();//read lock Object value = null; try {value = Cache.get (key);//query internal memory for value if (value = = null) {//If not, go to the database and check the results into the internal memory/
/release read lock, write lock Rwl.readlock (). Unlock (); Rwl.writelock (). Lock();
try {if (value = = null) {//re-judge to prevent multiple write threads from plugging in this place repeatedly write System.out.println ("read data from Database");
Value = "John";
Cache.put (key, value);
finally {//Setup completes release write lock Rwl.writelock (). Unlock ();
//Resume read-write status Rwl.readlock (). Lock ();
}else{System.out.println ("read data from cache");
finally {Rwl.readlock (). unlock ();//release read lock} return value;
}
}
return Result: