The Java package implements a read-write lock operation:
Package Com.multithread.readwritelock;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executor;import Java.util.concurrent.executors;import Java.util.concurrent.locks.Lock; Import Java.util.concurrent.locks.readwritelock;import Java.util.concurrent.locks.reentrantreadwritelock;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.IOException;import Com.multithread.main.exampleinterface;import Com.multithread.readwrite.WriteThread; Public classReadwritelockexample extends Exampleinterface { PublicReadwritelock Mreadwritelock =NewReentrantreadwritelock (false); PublicFile Mfile =NULL; Public intMcAsh =10000; PublicCountdownlatch Mlatchdown =NewCountdownlatch (6); PrivateBoolean Mstopedflag =false; @Override Public voidStartdemo () {//TODO auto-generated Method StubMfile =NewFile ("H:\\project\\sst\\123.txt"); Try{mfile.createnewfile (); Executor Mececutor= Executors.newfixedthreadpool (2+4); Mececutor.execute (NewReader ("Reader1")); Mececutor.execute (NewReader ("Reader2")); Mececutor.execute (NewReader ("Reader3")); Mececutor.execute (NewReader ("Reader4")); Mececutor.execute (NewWriter ("Writer1", -)); Mececutor.execute (NewWriter ("Writer2", - the)); Mlatchdown.await(); System. out. println ("[Startdemo]"+"Demo Down"); } Catch(IOException |interruptedexception e) { //TODO auto-generated Catch blockE.printstacktrace (); } } Public classReader extends Thread { PublicString name =NULL; Boolean flag=true; Private intindex =0; PublicReader (String name) { This. Name =name; } @Override Public voidrun () { while(flag) {Try{Mreadwritelock.readlock ().Lock(); System. out. println ("[Reader]"+ name +"Start"); Thread.Sleep ((Long) (Math.random () * -)); if(!Mstopedflag) {System. out. println ("[Reader]"+ name +"get McAsh Now:"+McAsh); } Else{flag=false; } thread.sleep ((Long) (Math.random () * -)); System. out. println ("[Reader]"+ name +" Down"); Mreadwritelock.readlock (). Unlock (); Thread.Sleep ((Long) (Math.random () * +)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} mlatchdown.countdown (); } } Public classWriter extends Thread { PublicString name =NULL; Boolean flag=true; Private intindex =0; Private intCash =0; PublicWriter (String name,intAddcash) { This. Name =name; This. Cash =Addcash; } @Override Public voidrun () {System. out. println ("[Writer]"+ name +"Start"); while(flag) {Try{Mreadwritelock.writelock ().Lock(); System. out. println ("[Writer]"+ name +"Start"); Thread.Sleep ((Long) (Math.random () * -)); intOldcash =McAsh; if(McAsh <=0) {flag=false; Mstopedflag=true; } Else{McAsh+=cash; System. out. println ("[Writer]"+name+"operator cash old:"+ Oldcash +"To :"+McAsh); } thread.sleep ((Long) (Math.random () * -)); System. out. println ("[Writer]"+ name +" Down"); Mreadwritelock.writelock (). Unlock (); Thread.Sleep ((Long) (Math.random () * +)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} mlatchdown.countdown (); } }}
The features of the read-write lock are:
1. The writer is mutually exclusive with the reader
2. Each writer is mutually exclusive
3. The reader can simultaneously
Second-kill multithreading the 14th reader-writer problem following read-write lock Srwlock (cont.)