Home >Java >javaTutorial >Java multi-threaded readwritelock example of separation of reading and writing

Java multi-threaded readwritelock example of separation of reading and writing

黄舟
黄舟Original
2017-10-10 10:23:261460browse

This article mainly introduces the relevant content of readwritelock separation of Java multi-threading. The article involves specific example codes, which has certain reference value. Friends who need it can learn about it.

In multi-threaded development, there is often a situation where we want toseparate reading and writing. For the reading action, multiple threads can read the resource at the same time, but for the writing action, only one thread can operate at the same time, and at the same time, when there is a writing thread operating this When accessing a resource, other reading threads cannot operate this resource. This greatly maximizes the characteristics of multi-threading and can fully utilize the capabilities of multi-threading.

In Java, the ReadWriteLock interface realizes this requirement for us. Through its implementation class ReentrantReadWriteLock, we can easily achieve the effect just now. Let's use an example to illustrate this class. usage.


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() { 
    //读取资源锁定 
    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 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; 
  } 
  @Override 
  public 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; 
  } 
  @Override 
  public 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(); 
  } 
}

Summarize

The above is the detailed content of Java multi-threaded readwritelock example of separation of reading and writing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn