锁所提供的最重要的改进之一就是 ReadWriteLock 接口和它的实现类 ReentrantReadWriteLock。这个类提供两把锁,一把用于读操作和一把用于写操作。同一时间可以有多个线程执行读操作,但只有一个线程可以执行写操作。当一个线程正在执行一个写操作,不可能有任何线程执行读操作。

public class VisitCounter {

    private ReadWriteLock lock;

    private long counter;

    public VisitCounter() {
counter = 0;
lock = new ReentrantReadWriteLock();
} public long get() {
long result = -1;
lock.readLock().lock();
try {
// System.out.println(Thread.currentThread().getName() + "准备查询访客计数器……" + ", " + new Date());
result = queryCounter();
// System.out.println(Thread.currentThread().getName() + "查得访客计数器值为" + result + ", " + new Date());
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
return result;
} public void increase() {
lock.writeLock().lock();
try {
// System.out.println("新增访客:" + Thread.currentThread().getName() + ", " + new Date());
updateCounter();
// System.out.println(Thread.currentThread().getName() + "更新访客计数器完毕……" + ", " + new Date());
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}
} private long queryCounter() throws Exception {
Thread.sleep((long)(Math.random() * 10000));
return counter;
} private void updateCounter() throws Exception {
Thread.sleep((long)(Math.random() * 10000));
counter++;
}
}

正如前面提及到的,ReentrantReadWriteLock 类有两把锁,一把用于读操作,一把用于写操作。用于读操作的锁,是通过在 ReadWriteLock 接口中声明的 readLock() 方法获取的。这个锁是实现 Lock 接口的一个对象,所以我们可以使用 lock(),unlock() 和 tryLock() 方法。用于写操作的锁,是通过在 ReadWriteLock 接口中声明的 writeLock() 方法获取的。这个锁是实现 Lock 接 口的一个对象,所以我们可以使用 lock(),unlock() 和 tryLock() 方法。确保正确的使用这些锁,使用它们与被设计的目的是一样的,这是程序员的职责。当获得 Lock 接口的读锁时,不能修改变量的值。否则,可能会有数据不一致的错误。

Java Concurrency - ReadWriteLock & ReentrantReadWriteLock的更多相关文章

  1. 《Java Concurrency》读书笔记,使用JDK并发包构建程序

    1. java.util.concurrent概述 JDK5.0以后的版本都引入了高级并发特性,大多数的特性在java.util.concurrent包中,是专门用于多线并发编程的,充分利用了现代多处 ...

  2. 深入浅出 Java Concurrency (15): 锁机制 part 10 锁的一些其它问题

      主要谈谈锁的性能以及其它一些理论知识,内容主要的出处是<Java Concurrency in Practice>,结合自己的理解和实际应用对锁机制进行一个小小的总结. 首先需要强调的 ...

  3. 深入浅出 Java Concurrency (15): 锁机制 part 10 锁的一些其它问题[转]

    主要谈谈锁的性能以及其它一些理论知识,内容主要的出处是<Java Concurrency in Practice>,结合自己的理解和实际应用对锁机制进行一个小小的总结. 首先需要强调的一点 ...

  4. Java Concurrency in Practice 读书笔记 第十章

    粗略看完<Java Concurrency in Practice>这部书,确实是多线程/并发编程的一本好书.里面对各种并发的技术解释得比较透彻,虽然是面向Java的,但很多概念在其他语言 ...

  5. Java Concurrency - 浅析 CountDownLatch 的用法

    The Java concurrency API provides a class that allows one or more threads to wait until a set of ope ...

  6. Java Concurrency - 浅析 CyclicBarrier 的用法

    The Java concurrency API provides a synchronizing utility that allows the synchronization of two or ...

  7. Java Concurrency - 浅析 Phaser 的用法

    One of the most complex and powerful functionalities offered by the Java concurrency API is the abil ...

  8. Java Concurrency - 线程执行器

    Usually, when you develop a simple, concurrent-programming application in Java, you create some Runn ...

  9. Java Concurrency - Callable & Future

    One of the advantages of the Executor framework is that you can run concurrent tasks that return a r ...

随机推荐

  1. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  3. UVaLive 7374 Racing Gems (DP,LIS)

    题意:以辆赛车可以从x轴上任意点出发,他的水平速度允许他向每向上移动v个单位,就能向左或向右移动v/r个单位(也就是它的辐射范围是个等腰三角形) 现在赛车从x轴出发,问它在到达终点前能吃到的最多钻石. ...

  4. 贪心-poj-2437-Muddy roads

    题目链接: http://poj.org/problem?id=2437 题目意思: 给n个区间,每次可以用长度为L的棒A去覆盖,求将所有区间覆盖至少需要几次.给的区间不覆盖. 解题思路: 简单贪心. ...

  5. 一次线上OOM故障排查经过

    转贴:http://my.oschina.net/flashsword/blog/205266 本文是一次线上OOM故障排查的经过,内容比较基础但是真实,主要是记录一下,没有OOM排查经验的同学也可以 ...

  6. Ribbon 窗体的 MDI 子窗体使用 TabbedMDIManager 切换时工具条闪屏问题的解决办法

    补充说明: 此问题已经在新版本中解决(15.2.6),方法更加简单,只需要在 MDIChild 窗体的 Create 方法中,将 Ribbon 的 Visible 属性设置为 false 就可以了,且 ...

  7. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  8. goldengate的HANDLECOLLISIONS参数

    HANDLECOLLISIONS 是一个 replicat 进程参数,主要在 initial load 中使用. 在 replicat 进程中使用该参数时,即使目标数据库环境中存在数据完整性问题(如 ...

  9. [Ramda] Basic Curry with Ramda

    var _ = R; /***************************************** C U R R Y I N G E X A M P L E **************** ...

  10. [AngularJS] Consistency between ui-router states and Angular directives

    ui-router's states and AngularJS directives have much in common. Let's explores the similarities bet ...