Multithreading implementation of Java multi-thread ~~~readwritelock read-write separation

Source: Internet
Author: User

In multithreaded development, there is often a situation where we want to read and write separate. Is that for reading this action, you can have multiple lines at the same time Chengtong

To read this resource, but for writing this action, you can only have one thread at a time, and at the same time, when there is a write thread operating the

Source, the other reading thread is not able to operate this resource, so that the full use of multi-threaded features, can be a good multi-threaded ability to play

Come out.

In Java, readwritelock this interface for us to achieve this demand, through his implementation of the class Reentrantreadwritelock we can

To implement the effect just now, let's use an example to illustrate the usage of this class.


Package Com.bird.concursey.charpet3;import Java.util.concurrent.locks.readwritelock;import Java.util.concurrent.locks.reentrantreadwritelock;public class Pricesinfo {private double price1;private double Price2;private readwritelock lock;public pricesinfo () {Price1 = 1.0;price2 = 2.0;lock = new Reentrantreadwritelock ();} Public double GetPrice1 () {//Read resource lock Lock.readlock (). Lock ();d ouble value = Price1;lock.readlock (). Unlock (); return value ;} Public double GetPrice2 () {Lock.readlock (). Lock ();d ouble value = Price2;lock.readlock (). Unlock (); return value;} public void Setprices (double Price1, double price2) {Lock.writelock (). Lock (); This.price1 = Price1;this.price2 = Price2; Lock.writelock (). Unlock ();}}


Here are two different classes for reading and writing


Package Com.bird.concursey.charpet3;public class Reader implements Runnable {private Pricesinfo Pricesinfo;public Reader (Pricesinfo pricesinfo) {this.pricesinfo = Pricesinfo;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.printf ("%s:price 1:%f\n", Thread.CurrentThread () . GetName (), Pricesinfo.getprice1 ()); System.out.printf ("%s:price 2:%f\n", Thread.CurrentThread (). GetName (), Pricesinfo.getprice2 ());}}}


Package Com.bird.concursey.charpet3;public class Writer implements Runnable {private Pricesinfo Pricesinfo;public Writer (Pricesinfo pricesinfo) {this.pricesinfo = Pricesinfo;} @Overridepublic void Run () {for (int i = 0; i < 3; i++) {System.out.printf ("writer:attempt to modify the prices.\n");p Ricesinfo.setprices (Math.random () *, Math.random () * 8); System.out.printf ("Writer:prices has been modified.\n"); try {thread.sleep (2);} catch (Interruptedexception e) { E.printstacktrace ();}}} public static void Main (string[] args) {Pricesinfo pricesinfo = new Pricesinfo (); Reader readers[] = new READER[5];  Thread threadsreader[] = new Thread[5];for (int i = 0; i < 5; i++) {readers[i] = new Reader (pricesinfo); Threadsreader[i] = new Thread (Readers[i]);} Writer writer = new writer (pricesinfo); Thread threadwriter = new Thread (writer), for (int i = 0; i < 5; i++) {Threadsreader[i].start ();} Threadwriter.start ();}}


Multithreading implementation of Java multi-thread ~~~readwritelock read-write separation

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.