Java高并发程序-Chapter3 JDK并发包(第十二讲)同步控制之 ReadWriteLock

1. 访问情况

读-读不互斥:读读之间不阻塞。
读-写互斥:读阻塞写,写也会阻塞读。

写-写互斥:写写阻塞。


2.主要接口

private static ReentrantReadWriteLock readWriteLock=new ReentrantReadWriteLock();
private static Lock readLock = readWriteLock.readLock();

private static Lock writeLock = readWriteLock.writeLock();

3. 程序实例

package com.john.learn.high.concurent.ch03;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo {

  private int value;

  private static ReentrantReadWriteLock ReadWriteLock = new ReentrantReadWriteLock();

  private static Lock Lock = new ReentrantLock();

  private Lock readLock = null;
  private Lock writeLock = null;

  public ReadWriteLockDemo(Lock readLock, Lock writeLock) {
    this.readLock = readLock;
    this.writeLock = writeLock;
  }

  public ReadWriteLockDemo(ReentrantReadWriteLock readWriteLock) {
    this.readLock = readWriteLock.readLock();
    this.writeLock = readWriteLock.writeLock();
  }

  public Object read() throws InterruptedException {

    try {

     readLock.lock();

     Thread.sleep(100);

     return value;

    } finally {

     readLock.unlock();

    }

  }

  public void write(int value) throws InterruptedException {

    try {

     writeLock.lock();

     Thread.sleep(100);

     this.value = value;

    } finally {
     writeLock.unlock();

    }
  }

  public static void main(String[] args) throws InterruptedException {

    Thread[] threads = new Thread[500];

    doTestCost("ReentrantLock", threads, new ReadWriteLockDemo(Lock, Lock));

    doTestCost("ReentrantReadWriteLock", threads, new ReadWriteLockDemo(ReadWriteLock));
  }

  private static void doTestCost(String targetName,  Thread[] threads, final ReadWriteLockDemo readWriteLockDemo)
     throws InterruptedException {
    
    for (int i = 0; i < threads.length; i++) {

     if (i < threads.length - 2) {
       threads[i] = new Thread() {

         public void run() {

          try {
            readWriteLockDemo.read();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

         };
       };

       continue;
     }

     threads[i] = new Thread() {

       public void run() {

         try {
          readWriteLockDemo.write(new Random().nextInt());
         } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }

       }
     };

    }

    long start = System.currentTimeMillis();

    for (Thread thread : threads) {

     thread.start();
    }

    for (Thread thread : threads) {

     thread.join();
    }

    System.out.println(targetName + " cost is " + (System.currentTimeMillis() - start)  +" .");
  }

}
 

使用读写锁 和 重用锁的性能对比:498个Thread 读 2个Thread 写,每一个操作耗时 100 毫秒

 读写锁比重用锁快100多倍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值