Package Cn.dyg;import Java.util.hashmap;import Java.util.map;import java.util.concurrent.locks.condition;import Java.util.concurrent.locks.reentrantlock;import Java.util.concurrent.locks.reentrantreadwritelock;import Java.util.concurrent.locks.reentrantreadwritelock.readlock;import Java.util.concurrent.locks.reentrantreadwritelock.writelock;public class Threadlock {public static void main (String[ ] args) {final business business = new Businesswritereadlock (), for (int i = 0; i < 3; i++) {New Thread (new Runnable () { @Overridepublic void Run () {while (true) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Business.get ("Zjy"));}}). Start ();} New Thread (New Runnable () {@Overridepublic void run () {try {thread.sleep ()} catch (Interruptedexception e) {e.prints Tacktrace ();} Business.put ("Zjy", "Zhaojunyang");}). Start ();}} Interface Business {public void put (string key, String value);p ublic string Get (string key); Class businessnosynchronized IMplements Business {Private map<string, string> Map = new hashmap<string, string> (), @Overridepublic Void put ( String key, String value) {System.out.println (Thread.CurrentThread (). GetName () + "Write shared data ..."); Map.put (Key, value);} @Overridepublic string Get (String key) {System.out.println (Thread.CurrentThread (). GetName () + "read shared data ..."); return Map.get (key);}} Class Businesssynchroinzed implements business {private map<string, string> Map = new hashmap<string, string> (); @Overridepublic synchronized string get (String key) {String value = Map.get (key); while (value = = null) {try {wait (); Val UE = Map.get (key);} catch (Interruptedexception e) {e.printstacktrace ();}} return Map.get (key);} @Overridepublic synchronized void put (string key, String value) {Map.put (key, value); notify ();}} Class Businesslock implements business {private map<string, string> Map = new hashmap<string, string> ();p Riva Te reentrantlock lock = new Reentrantlock (); Private ConditIon condition = Lock.newcondition ();p ublic void put (string key, String value) {Lock.lock (); try {System.out.println ( Thread.CurrentThread (). GetName () + "Write shared data ..."); Map.put (key, value); Condition.signal ();} finally {Lock.unlock ();}} public string get (string key) {lock.lock (); try {System.out.println (Thread.CurrentThread (). GetName () + "read shared data ... "); while (Map.get (key) = = null) {try {condition.await ()} catch (Interruptedexception e) {e.printstacktrace ()}} return Map.get (key);} finally {Lock.unlock ();}}} This is a legacy problem, how to achieve blocking and release read and write locks, this has been more confused, hoping to pass the great God to give guidance?? Class Businesswritereadlock implements business {private map<string, string> Map = new hashmap<string, String> ;();p rivate reentrantreadwritelock lock = new Reentrantreadwritelock ();p rivate writelock writelock = Lock.writelock (); Private Readlock Readlock = Lock.readlock (); @Overridepublic void put (string key, String value) {Writelock.lock (); try {map . put (key, value);} finally {Writelock.unlock ();}} @Overridepublic String GET (String key) {readlock.lock (); try {while (Map.get (key) = = null) {Thread.yield ();//does not release the current lock}return map.get (key);} finally {Readlock.unlock ();}}}
Multi-threaded synchronization