首页 >Java >java教程 >Java多线程之readwritelock读写分离的实例

Java多线程之readwritelock读写分离的实例

黄舟
黄舟原创
2017-10-10 10:23:261429浏览

这篇文章主要介绍了Java多线程之readwritelock读写分离的相关内容,文中涉及具体实例代码,具有一定参考价值,需要的朋友可以了解下。

在多线程开发中,经常会出现一种情况,我们希望读写分离。就是对于读取这个动作来说,可以同时有多个线程同时去读取这个资源,但是对于写这个动作来说,只能同时有一个线程来操作,而且同时,当有一个写线程在操作这个资源的时候,其他的读线程是不能来操作这个资源的,这样就极大的发挥了多线程的特点,能很好的将多线程的能力发挥出来。

在Java中,ReadWriteLock这个接口就为我们实现了这个需求,通过他的实现类ReentrantReadWriteLock我们可以很简单的来实现刚才的效果,下面我们使用一个例子来说明这个类的用法。


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(); 
  } 
}

下面是读和写两个不同的类


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(); 
  } 
}

总结

以上是Java多线程之readwritelock读写分离的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn