Java multithreading ~~~ Multi-thread Implementation of ReadWriteLock read/write splitting

Source: Internet
Author: User

Java multithreading ~~~ Multi-thread Implementation of ReadWriteLock read/write splitting

In multi-threaded development, there is often a situation where we want read/write splitting. For reading this action, there can be multiple threads at the same time

But for writing this action, only one thread can operate at the same time, and at the same time, when a write thread operates this resource

When the source is used, other read threads cannot operate on this resource. In this way, the multi-thread feature is brought into full play and the multi-thread capability can be well utilized.

.

In Java, the ReadWriteLock interface implements this requirement for us. Through its implementation class ReentrantReadWriteLock, we can

To achieve the effect just now, we will 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. 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 ();}}


The following are two different read and write classes.


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 < 10; 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");pricesInfo.setPrices(Math.random() * 10, Math.random( ) * 8);System.out.printf("Writer: Prices have 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();}}


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.