/* * Reader Writer Questions * * */class Buffer{ public string buffer[] = new string[10];//used to identify a shared buffer The public boolean flag = false;//is used to identify the state of the buffer public int i=0;//represents the position written to the buffer }class writer implements runnable // There may be multiple read { private buffer buffer; public writer (Buffer buffer) { this.buffer = buffer; } @Override public void run () { while (TRUE)//use a circular representation to keep writing to the inside { synchronized (buffer)//This is a synchronous code block, which is the operation of shared data, only one thread operation { while (Buffer.flag)//If shared data flag=true, indicates that the data is already written in the shared data block has not been read out, If you write again, you have to wait, it is also the key to control reading once written { try { buffer.wait (); } catch (Interruptedexception ex) { system.out.println (ex.getMessage ()); ex.printstacktrace (System.out); } } if (buffer.i == Buffer.buffer.length) { &Nbsp; system.out.println (" Buffer is full, empty and then write "); buffer.buffer = new String[10]; buffer.i = 0; } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BUFFER.BUFFER[BUFFER.I] = "string" + buffer.i;//this is the operation that writes data to the buffer system.out.println (Thread.currentThread (). GetName ( ) + "to" + buffer.i + "write:" + buffer.buffer[buffer.i]); buffer.i = buffer.i + 1;//prevent overflow of data buffer.flag = true; buffer.notifyall (); } try { thread.sleep (+); } catch (Exception ex) { System.out.println (Ex.getmessage ()); &NBsp; Ex.printstacktrace (System.out); } } }}class Reader implements Runnable{ private buffer buffer; public reader ( Buffer buffer) { this.buffer = buffer; } @Override public Void run () { while (True) //says Readers are constantly reading the operation { synchronized (buffer) { while (! Buffer.flag) { try { buffer.wait (); } catch ( INTERRUPTEDEXCEPTION&NBSP;EX) { &nBsp; system.out.println (Ex.getmessage ()); ex.printstacktrace (System.out); } } system.out.print (Thread.CurrentThread (). GetName () + " read:"); for (int i = 0; i < buffer.i; i++) { system.out.print (Buffer.buffer[i]); } system.out.println (); buffer.flag = false; buffer.notifyall (); } try { thread.sleep (100);// This sentence does not mean anything, just to let the output will be slower, there is no other role in this program, add not all the same } catch (Interruptedexception ex) { System.out.println (Ex.getmessage ()); ex.printstacktrace (System.out); } } }}/* * The above program in the case of only a reader and a writer without any errors, the writer did not write into a data, the reader read the data once * In practice, the number of threads can be many, and in the case of multiple readers and multiple writes, the code above will now have a new problem--- * that is to write one and read it multiple times, or read it once and write it multiple times, this will be a security issue, let's discuss why this problem occurs * FakeNow there are two readers and two written by the reader, and the thread that is now executing is the readers, and the two writer wait , after the reader executes, wakes one of the writer, and then the writer begins to execute * when the writer completes, the following starts to wake up the thread, because the above two read threads and a write thread are waiting, if the wake is a write thread, because it uses * if (Buffer.flag) This has been judged before wait , So the following statement is executed directly, then it is read one time, and the connection is written two times, if the thread written more words, * then there will be more problems, so the above if (Buffer.flag) is changed to while (Buufer.flag) buffer.notify Change to Buffer.notifyall (); that's right. * * The above program is controlled by while (flag) notifyAll , so no matter The number of writer threads or reader threads, whether or not they are the same, they will follow the order of reading a write one * */public class readerandwriter{ public static void main (string args[]) { buffer buffer = new buffer (); thread t1 = new thread (new writer (buffer)); thread t2 = new thread (new writer (buffer)); thread t3 = new thread (New reader (buffer)); thread t4 = new thread (New Reader (buffer)); t1.start (); t2.start ( ); t3.start (); t4.start (); }}
Reader Writer Pattern example