Reader writer-[writer first]

Source: Internet
Author: User
1. Implementation Principle

First, design two threads, one reader thread, one writer thread, and two threads.

The reader writer's problem is that two types of operations (read and write) can be performed on resources ). Write operations are exclusive (excluding readers and other writers), and read operations are shared (multiple readers are readable and writers are excluded ). There are two strategies: 1. reader first (strong reader synchronization): always give the reader priority. As long as the writer does not perform write operations, the reader can gain access. This situation exists in many readers and is often used when writers are not updated. It is better for the readers to use the library reference database. 2. Writer Priority (strong writer synchronizes strong writer synchronization): Generally, priority is given to the writer, and the reader is delayed until all the writers waiting for or activities are completed. This situation exists in a frequently updated system, and the reader is interested in the latest information, such as the air ticket booking system. The writer updates the air ticket price and the reader obtains the current fare. Simply put, if no resource is occupied, the user first uses the lock, the reader locks, and the writer locks the reader when using the lock. 1. let's assume that the reader first locks the reader, and then a) the reader can read the resource, and the reader counter is + 1, B) the writer will wait (block or keep trying) until the reader releases the resource (the counter is 0), select a writer to start writing 2. if the writer locks are first introduced, for example, N readers and M writers are waiting (The writer locks exclusive). If a reader gives priority to the writer, the writer releases the lock, let the waiting n readers read the data first (other writers wait and wait for it to change to Case 1). If B is given priority, the writer releases the lock, let one of the waiting M writers write, and the reader waits until there are no writers (the waiting M writers finish writing and come back to the writers in the middle ).
Therefore, the premise for discussing whether the reader is preferred or whether the writer is preferred is that the writer locks the reader on the writer and the writer is starved to death.

2. Code above:

Buffer class

Package COM. bankht. writerreader;/*** @ author zhuyong * @ Creation Time: 02:32:02 ** @ Class description: Buffer */public class cubbyhole {private int readercount; // number of readers private int writercount; // number of writers private Boolean dbreading; // read semaphores private Boolean dbwriting; // write semaphores public cubbyhole () {readercount = 0; writercount = 0; dbreading = false; dbwriting = false;} public static void napping () {// thread sleep without consuming CPU resources try {thread. sleep (INT) (math. random () * 4000);} catch (exception e) {}} public synchronized int startread () {// start reading while (writercount> 0) {try {system. out. println ("Reader is waiting"); wait (); // wait for the writer to issue catch (exception e) {}}+ readercount; If (readercount> = 1) {dbreading = true;} return readercount;} public synchronized int endreading () {// end read -- readercount; If (readercount = 0) {dbreading = false ;} policyall (); system. out. println ("One reader is done reading. count = "+ readercount); Return readercount;} public synchronized void startwriting () {// start write + + writercount; while (dbreading = true | dbwriting = true) {try {system. out. println ("writer is waiting"); wait (); // wait for the reader to issue catch (exception e) {}} dbwriting = true;} public synchronized void endwriting () {// end write -- writercount; dbwriting = false; system. out. println ("One writer is done writing. count = "+ writercount); policyall ();}}

Readers:

Package COM. bankht. writerreader;/*** @ author zhuyong * @ Creation Time: 02:32:51 ** @ Class description: reader */public class reader extends thread {// defines the read thread, inherit the Thread class, override the run method private cubbyhole C; private int readernum; Public reader (int r, cubbyhole dB) {readernum = r; C = dB;} public void run () {int C; while (true) {system. out. println ("Reader" + readernum + "is sleeping"); cubbyhole. napping (); system. out. println ("Reader" + readernum + "wants to read"); C = C. startread (); system. out. println ("Reader" + readernum + "is reading. count = "+ C); cubbyhole. napping (); C = C. endreading (); system. out. println ("It is Reader" + readernum + "who has done reading. count = "+ C );}}}

Writer class:

Package COM. bankht. writerreader;/*** @ author zhuyong * @ Creation Time: 02:34:36, December 7, ** @ Class description: writer */public class writer extends thread {// defines the write thread private cubbyhole C; private int writernum; Public writer (int w, cubbyhole dB) {writernum = W; C = dB;} public void run () {While (true) {system. out. println ("Writer" + writernum + "is sleeping"); cubbyhole. napping (); system. out. println ("Writer" + writernum + "wants to write"); C. startwriting (); system. out. println ("Writer" + writernum + "is writing"); cubbyhole. napping (); C. endwriting (); system. out. println ("It is writer" + writernum + "who has done writing. ");}}}

Test class:

Package COM. bankht. writerreader;/*** @ author zhuyong * @ Creation Time: 02:31:32 ** @ Class description: */public class writerreader {public static void main (string [] ARGs) {testwriterreader ();} public static void testwriterreader () {cubbyhole DB = new cubbyhole (); // buffer reader R1 = new reader (1, DB ); // reader r2 = new reader (2, DB); reader R3 = new reader (3, DB); reader r4 = new reader (4, DB ); writer W1 = new writer (1, DB); // writer W2 = new writer (2, DB); r1.start (); r2.start (); r3.start (); w1.start (); r4.start (); w2.start ();}}

Running result:

reader 1 is sleepingreader 3 is sleepingreader 2 is sleepingreader 4 is sleepingWriter 1 is sleepingWriter 2 is sleepingWriter 1 wants to writeWriter 1 is writingreader 1 wants to readreader is waitingreader 2 wants to readreader is waitingone writer is done writing. Count=0It is Writer 1 who has done writing .reader 1 is reading. Count=2reader 2 is reading. Count=1Writer 1 is sleepingreader 3 wants to readreader 3 is reading. Count=3Writer 1 wants to writeWriter is waitingWriter 2 wants to writeWriter is waitingone reader is done reading. Count=2Writer is waitingIt is reader 1 who has done reading. count=2Writer is waitingreader 1 is sleepingreader 4 wants to readreader is waitingone reader is done reading. Count=1It is reader 2 who has done reading. count=1reader is waitingreader 2 is sleepingWriter is waitingWriter is waitingreader 1 wants to readreader is waitingone reader is done reading. Count=0It is reader 3 who has done reading. count=0reader is waitingreader 3 is sleepingWriter is waitingWriter 2 is writingreader is waitingreader 2 wants to readreader is waitingone writer is done writing. Count=1reader is waitingIt is Writer 2 who has done writing .reader is waitingWriter 2 is sleepingreader is waitingWriter 1 is writingreader 3 wants to readreader is waitingWriter 2 wants to writeWriter is waitingone writer is done writing. Count=1It is Writer 1 who has done writing .reader is waitingWriter 2 is writingreader is waitingWriter 1 is sleepingreader is waitingreader is waiting 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.