Java multi-thread Reader

Source: Internet
Author: User

 

/** Reader issue **/import Java. util. concurrent. locks. *; Class Buffer {public int buffer [] = new int [10]; // public Boolean flag used to identify the shared buffer = false; // used to identify the buffer State Public int I; // indicates the position written to the buffer} Class Writer implements runnable // There may be multiple reads {private buffer; public writer (buffer) {This. buffer = buffer;} public void run () {While (true) // use a loop to keep writing to It {synchronized (buffer) // synchronous Code Block, which are all operations on shared data. Only one thread operation can be performed {While (buffer. flag) // if the shared data flag is true, it indicates that the data in the shared data block has not been read. If the data is written again, wait, this is also the key try {buffer to control one write operation. wait ();} catch (exception ex) {} buffer. buffer [buffer. i] = buffer. i; // The operation buffer for writing data to the buffer. I = (buffer. I + 1) % buffer. buffer. length); // prevent data overflow buffer. flag = true; buffer. policyall ();} Try {thread. sleep (100);} catch (exception ex) {}}} class reader implements runnable {private buffer; Pub LIC reader (buffer) {This. buffer = buffer;} public void run () {While (true) // indicates that the reader keeps reading {synchronized (buffer) {While (! Buffer. flag) Try {buffer. wait () ;}catch (exception ex) {}for (INT I = 0; I <buffer. i; I ++) {system. out. print (buffer. buffer [I]);} system. out. println (); buffer. flag = false; buffer. policyall ();} Try {thread. sleep (100);} catch (exception ex) {}// this sentence is meaningless, just to make the output slower. Program There is no other function in it. add or remove a sample.} public class readerandwriter {public static void main (string ARGs []) {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 ();}} /** the above program has no errors when there is only one reader and one writer. If the writer does not write a data record, the reader reads the data once. * In practice, there may be many types of threads. When there are multiple readers and multiple writers, the above code will now have a new problem-* That is, writing one and Reading multiple times, or reading one and writing multiple times, which will lead to security issues, next we will discuss why this problem occurs * assume that there are two readers and two writers, and now the execution thread is the reader. In the two writers of wait, after the reader completes the execution, it will wake up one of the writers, then the writer starts to execute * after the writer completes the execution, the following will start to wake up the thread again, because the two read threads and one write thread above are waiting, if the wake-up is followed by a write thread, because * If (buffer. flag) This has been determined before wait, so the following statement will be executed directly, that is, when you read it once, and the connection is written twice, if more threads are written, * more problems will occur, so the above if (buffer. flag) to be changed to while (buufer. flag) buffer. Y should be changed to buffer. notifyall (); in this way, the preceding program is correctly controlled by whiie (FLAG) notifyall. Therefore, no matter how many writer threads or reader threads are there, they all follow the order of reading, writing, and writing ****/

Related Article

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.