Java implementations:
The key to this problem is the synchronization between reader and writer, especially the use of Java to operate.
1. Wait for the reader, use Countdownlatch mreaderlatch, but Countdownlatch can only be used once, so you need to new one each time.
Or you might consider using semaphore instead, but Semaphore needs acquire (Read_thread_size) to wait for all reader threads to end.
2. Wait for the write operation. Use semaphore to control, mwritesema.release (read_thread_size); Indicates that the reader can operate at the same time.
The key code is as follows:
Public voidWaitreaderend () {//multiple read end,using countdownlatch Try{mreaderlatch.await (); Mreaderlatch=NewCountdownlatch (read_thread_size); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidSingalwriteend () {mwritesema.release (read_thread_size); } Public voidWaitwriteend () {Try{mwritesema.acquire (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidSingalreadend () {Mreaderlatch.countdown (); } Public voidInitreadnone () {Try{mwritesema.acquire (read_thread_size); for(inti=0;i<read_thread_size;i++) {Mreaderlatch.countdown (); } } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
The detailed code is as follows:
PackageCom.multithread.readwrite;Importjava.util.ArrayList;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Queue;Importjava.util.concurrent.BrokenBarrierException;ImportJava.util.concurrent.CountDownLatch;ImportJava.util.concurrent.CyclicBarrier;ImportJava.util.concurrent.Executor;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.Semaphore;ImportCom.multithread.main.ExampleInterface;ImportCom.multithread.prosumer.ProducerThread;/*** 1. The writer waits for all readers to finish reading before continuing to write. * 2. All readers will have to wait until the writer finishes writing to continue reading. * 3. The reader can share the read file. * * * * */ Public classReaderwriterexampleextendsExampleinterface { Public Static Final intRead_thread_size = 4; Public Static Final intBuffer_length = 100; PublicList<integer> G_productor =NewArraylist<integer>(); PublicCountdownlatch Mreaderlatch =NewCountdownlatch (read_thread_size); PublicSemaphore Mwritesema =NewSemaphore (read_thread_size); Public BooleanBstopflag =false; PublicCountdownlatch Mlatchdown =NewCountdownlatch (1 +read_thread_size); PublicCountdownlatch Mlatchstart =NewCountdownlatch (1 +read_thread_size); @Override Public voidStartdemo () {//TODO auto-generated Method Stub Try{initreadnone (); Bstopflag=false; Executor Mececutor= Executors.newfixedthreadpool (1 +read_thread_size); Mececutor.execute (NewWritethread ( This, "Writer")); for(inti=1;i<=read_thread_size;i++) {Mececutor.execute (NewReadthread ( This, "Reader" +i)); } mlatchstart.await (); System.out.println ("All Thread is runnning"); Mlatchdown.await (); System.out.println ("All Thread was down"); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidWaitreaderend () {//multiple read end,using countdownlatch Try{mreaderlatch.await (); Mreaderlatch=NewCountdownlatch (read_thread_size); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidSingalwriteend () {mwritesema.release (read_thread_size); } Public voidWaitwriteend () {Try{mwritesema.acquire (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidSingalreadend () {Mreaderlatch.countdown (); } Public voidInitreadnone () {Try{mwritesema.acquire (read_thread_size); for(inti=0;i<read_thread_size;i++) {Mreaderlatch.countdown (); } } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
PackageCom.multithread.readwrite; Public classReadthreadextendsThread {readerwriterexample mrwexample=NULL; String name=NULL; BooleanFlag =true; PublicReadthread (readerwriterexample re,string name) {mrwexample=re; This. Name =name; } @Override Public voidrun () {MRwExample.mLatchStart.countDown (); while(flag) {//wait for write to endMrwexample.waitwriteend (); //read the file until the end of this session//whether the check file write operation has completely ended and ends the read thread. intMreadlength =mRwExample.g_productor.size (); String Mreadstr= ""; if(mreadlength>0) { for(Integer a:mrwexample.g_productor) {mreadstr+=string.format ("%x", a); } System.out.println (Name+ "Read data:" +mreadstr); } if(mrwexample.bstopflag) {flag=false; } //Notice the end of this reader. Mrwexample.singalreadend (); } System.out.println (Name+ "Read Data End"); MRwExample.mLatchDown.countDown (); } }
PackageCom.multithread.readwrite; Public classWritethreadextendsThread {readerwriterexample mrwexample=NULL; String name=NULL; intIflag = 0; PublicWritethread (readerwriterexample re,string name) {mrwexample=re; This. Name =name; } @Override Public voidrun () {MRwExample.mLatchStart.countDown (); intindex = 0; while(index<readerwriterexample.buffer_length) { //waits for all readers to end the read operation. Mrwexample.waitreaderend (); intMwritelength = (int) (Math.random () *9) +1;//1-10; if(Mwritelength > (Readerwriterexample.buffer_length-index)) {Mwritelength= Readerwriterexample.buffer_length-index; } //writing data to a file at the end//Check writes data to the last. mRwExample.g_productor.clear (); intMparam = 0; String WriteLine= ""; for(inti=0;i<mwritelength;i++) {Mparam= (int) (Math.random () *14) +1;//0-eMRwExample.g_productor.add (Mparam); WriteLine+ = String.Format ("%1$x", Mparam); } Index= index +mwritelength; SYSTEM.OUT.PRINTLN (Name+ "Write Data:" +writeline+ "\ T Current index:" +index); if(index>=readerwriterexample.buffer_length) {Mrwexample.bstopflag=true; } //notifies the end of a write operation that can be read. Mrwexample.singalwriteend (); Iflag++; } System.out.println (Name+ "End of thread operation"); MRwExample.mLatchDown.countDown (); } }
Second-kill multithreading 11th reader Writer's question (cont.)