Java read-write lock for data synchronization access

Source: Internet
Author: User

One of the biggest improvements to the lock mechanism is the Readwritelock interface and its only implementation class Reentrantreadwritelock. This class has two locks, one is a read operation lock and the other is a write operation lock. You can allow multiple threads to access at the same time using a read lock, but only one thread is allowed to use a write lock. When a thread performs a write operation, other threads are not able to perform a read operation.

Below we will learn how to use the Readwritelock interface to write programs using the example. This example uses the Readwritelock interface to control access to a price object, which stores the price of two products.

1. Create a price information class Pricesinfo, and store the price of two products.

Import Java.util.concurrent.locks.readwritelock;import Java.util.concurrent.locks.reentrantreadwritelock;public Class Pricesinfo {    //Two price    private double Price1;    Private double price2;    Declaring a read-write lock Readwritelock object lock    private readwritelock lock;    Public Pricesinfo () {        Price1 = 1.0;        Price2 = 2.0;        lock = new Reentrantreadwritelock ();    }        Public double GetPrice1 () {        lock.readlock (). Lock ();        Double value = Price1;        Lock.readlock (). Unlock ();        return value;    }        Public double GetPrice2 ()    {        lock.readlock (). Lock ();        Double 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 ();    }    }

2. Create a read-class reader, which implements the Runnable interface.

public class Reader implements Runnable {    private pricesinfo pricesinfo;    Public Reader (Pricesinfo pricesinfo) {        this.pricesinfo = pricesinfo;    }    @Override public    Void Run () {        //loop to read a price 10 times for        (int i=0;i<10;i++) {            System.out.printf ("%s:price1 :%f\n ", Thread.CurrentThread (). GetName (), Pricesinfo.getprice1 ());            System.out.printf ("%s:price2:%f\n", Thread.CurrentThread (). GetName (), Pricesinfo.getprice2 ());}}}    

3. Create write class writer, which implements the Runnable interface.

public class Writer implements Runnable {    private pricesinfo pricesinfo;    Public Writer (Pricesinfo pricesinfo) {        this.pricesinfo = pricesinfo;    }    @Override public    Void Run () {        //loop modify two Prices 3 times        try {for            (int i=0;i<3;i++) {                System.out.printf (" Writer:attempt to modify the prices.\n ");                Pricesinfo.setprices (Math.random () *10, Math.random () *8);                System.out.println ("Writer:prices has been modified.");                Thread.Sleep (2);            }        } catch (Interruptedexception e) {            //TODO auto-generated catch block            e.printstacktrace ();        }    }}

4. Create the main class of the example main

public class Main {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 ();    }}

5. The program operation results are as follows

Thread-1: price1:1.000000thread-4: price1:1.000000thread-2: price1:1.000000thread-2: price2:2.000000thread-2: Price1:1.000000thread-2: price2:2.000000thread-0: price1:1.000000thread-0: price2:2.000000thread-0: Price1: 1.000000thread-0: price2:2.000000thread-0: price1:1.000000thread-0: price2:2.000000thread-0: Price1: 1.000000thread-0: price2:2.000000thread-0: price1:1.000000thread-0: price2:2.000000thread-0: Price1: 1.000000thread-0: price2:2.000000thread-0: price1:1.000000thread-0: price2:2.000000thread-0: Price1: 1.000000thread-0: price2:2.000000thread-0: price1:1.000000thread-0: price2:2.000000thread-0: Price1: 1.000000thread-0: price2:2.000000thread-2: price1:1.000000thread-2: Price2:2.000000writer:attempt to modify the price S.writer:prices has been modified. Thread-3: price1:1.000000thread-3: price2:4.840562thread-1: price2:2.000000thread-1: price1:6.220535thread-1: Price2:4.840562thread-1: price1:6.220535thread-1: price2:4.840562thread-1: Price1:6.220535thread-1: price2:4.840562thread-1: price1:6.220535thread-1: price2:4.840562thread-1: Price1: 6.220535thread-1: price2:4.840562thread-1: price1:6.220535thread-1: price2:4.840562thread-1: Price1: 6.220535thread-1: price2:4.840562thread-1: price1:6.220535thread-1: price2:4.840562thread-1: Price1: 6.220535thread-1: Price2:4.840562writer:attempt to modify the prices. Writer:prices has been modified. Thread-4: price2:2.000000thread-4: price1:5.640719thread-4: price2:1.872038thread-4: price1:5.640719thread-4: Price2:1.872038thread-2: price1:6.220535thread-2: price2:1.872038thread-2: price1:5.640719thread-2: Price2: 1.872038thread-2: price1:5.640719thread-2: price2:1.872038thread-2: price1:5.640719thread-2: Price2: 1.872038thread-2: price1:5.640719thread-2: price2:1.872038thread-2: price1:5.640719thread-2: Price2: 1.872038thread-2: price1:5.640719thread-2: price2:1.872038thread-3: price1:5.640719thread-3: Price2: 1.872038thread-3: price1:5.640719thread-3: PrIce2:1.872038thread-3: price1:5.640719thread-3: price2:1.872038thread-3: price1:5.640719thread-3: Price2: 1.872038thread-3: price1:5.640719thread-3: price2:1.872038thread-3: price1:5.640719thread-3: Price2: 1.872038thread-3: price1:5.640719thread-3: price2:1.872038thread-3: price1:5.640719thread-3: Price2: 1.872038thread-3: price1:5.640719thread-3: Price2:1.872038writer:attempt to modify the prices. Writer:prices has been modified. Thread-4: price1:5.491746thread-4: price2:2.729420thread-4: price1:5.491746thread-4: price2:2.729420thread-4: Price1:5.491746thread-4: price2:2.729420thread-4: price1:5.491746thread-4: price2:2.729420thread-4: Price1: 5.491746thread-4: price2:2.729420thread-4: price1:5.491746thread-4: price2:2.729420thread-4: Price1: 5.491746thread-4: price2:2.729420

Java read-write lock for data synchronization access

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.