java并发初探ReentrantWriteReadLock

ReenWriteReadLock类的优秀博客

ReentrantReadWriteLock读写锁详解

Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock

ReentrantWriteReadLock类实现

@startuml
interface Lock
interface ReadWriteLock
class ReentrantReadWriteLock{
Sync sync
ReentrantReadWriteLock.ReadLock readerLock;
ReentrantReadWriteLock.WriteLock writerLock;
}
ReadWriteLock <|-- ReentrantReadWriteLock
ReentrantReadWriteLock --o ReadLock
ReentrantReadWriteLock --o WriteLock
ReentrantReadWriteLock --o Sync
AbstractQueuedSynchronizer <|- Sync
Lock <|-- ReadLock
Lock <|-- WriteLock
@enduml

特点

ReentrantWriteReadLock内部有读锁和写锁。

写锁是独占锁

当前线程持有写锁,其他线程只能等待

当其他线程拥有对象的写锁或者读锁,当前线程等待获取写锁

读锁是共享锁

没有其他线程的写锁,当前线程可以获取读锁

使用例子

package com.java.javabase.thread.base.concurrent.lock;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; @Slf4j
public class ReentranReadWriteLockTest {
/**
* @author
* @version
* @since
*/
public static void main(String[] args) {
ReentranReadWriteLockTest test =new ReentranReadWriteLockTest();
User user =test.new User("jack",test.new Count("001",1000));
for(int i=0;i<3;i++)
{
user.printCash();
user.setCash(1000);
}
} @Data
@AllArgsConstructor
@NoArgsConstructor
class Count {
private String id;
private int cash;
} @Data
class User {
private String name;
private Count count;
private ReadWriteLock readWriteLock; public User(String name, Count count) {
this.name = name;
this.count = count;
this.readWriteLock = new ReentrantReadWriteLock();
} public void setCash(final int cash) {
new Thread() {
@Override
public void run() {
readWriteLock.writeLock().lock();
try {
log.info("thread {} :user {} set cash {} start",
Thread.currentThread().getName(), name, cash);
Thread.sleep(1000);
log.info("thread {} :user {} current cash = {}",
Thread.currentThread().getName(), name, count.getCash());
count.setCash(cash);
log.info("thread {} :user {} set cash {} end",
Thread.currentThread().getName(), name, cash);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
readWriteLock.writeLock().unlock();
}
} }.start(); } public void printCash() {
new Thread() {
@Override
public void run() {
try {
readWriteLock.readLock().lock();
log.info("thread {} :user {} get cash start", Thread.currentThread().getName(), name);
log.info("thread {} :user {} current cash = {}",
Thread.currentThread().getName(), name, count.getCash());
Thread.sleep(1000);
log.info("thread {} :user {} get cash end", Thread.currentThread().getName(), name);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readWriteLock.readLock().unlock();
} }
}.start();
}
}
}

例子结果

2019-08-09 10:49:15,618   [Thread-0] INFO  ReentranReadWriteLockTest  - thread Thread-0 :user jack get  cash start
2019-08-09 10:49:15,618 [Thread-0] INFO ReentranReadWriteLockTest - thread Thread-0 :user jack current cash = 1000
2019-08-09 10:49:16,618 [Thread-0] INFO ReentranReadWriteLockTest - thread Thread-0 :user jack get cash end
2019-08-09 10:49:16,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack set cash 1000 start
2019-08-09 10:49:17,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack current cash = 1000
2019-08-09 10:49:17,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack set cash 1000 end
2019-08-09 10:49:17,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack get cash start
2019-08-09 10:49:17,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack get cash start
2019-08-09 10:49:17,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack current cash = 1000
2019-08-09 10:49:17,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack current cash = 1000
2019-08-09 10:49:18,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack get cash end
2019-08-09 10:49:18,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack get cash end
2019-08-09 10:49:18,618 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack set cash 1000 start
2019-08-09 10:49:19,617 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack current cash = 1000
2019-08-09 10:49:19,617 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack set cash 1000 end
2019-08-09 10:49:19,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack set cash 1000 start
2019-08-09 10:49:20,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack current cash = 1000
2019-08-09 10:49:20,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack set cash 1000 end

java并发初探ReentrantWriteReadLock的更多相关文章

  1. java并发初探ConcurrentSkipListMap

    java并发初探ConcurrentSkipListMap ConcurrentSkipListMap以调表这种数据结构以空间换时间获得效率,通过volatile和CAS操作保证线程安全,而且它保证了 ...

  2. java并发初探ConcurrentHashMap

    java并发初探ConcurrentHashMap Doug Lea在java并发上创造了不可磨灭的功劳,ConcurrentHashMap体现这位大师的非凡能力. 1.8中ConcurrentHas ...

  3. java并发初探ThreadPoolExecutor拒绝策略

    java并发初探ThreadPoolExecutor拒绝策略 ThreadPoolExecuter构造器 corePoolSize是核心线程池,就是常驻线程池数量: maximumPoolSize是最 ...

  4. java并发初探CyclicBarrier

    java并发初探CyclicBarrier CyclicBarrier的作用 CyclicBarrier,"循环屏障"的作用就是一系列的线程等待直至达到屏障的"瓶颈点&q ...

  5. java并发初探CountDownLatch

    java并发初探CountDownLatch CountDownLatch是同步工具类能够允许一个或者多个线程等待直到其他线程完成操作. 当前前程A调用CountDownLatch的await方法进入 ...

  6. java并发:初探sleep方法

    sleep与wait sleep是Thread方法,使得当前线程从运行态变为阻塞态.但它不会释放对象的锁. wait方法是Object方法,它的作用是使得当前拥有对象锁的线程从运行态变为阻塞态, 它会 ...

  7. java并发队列

    阻塞队列 常见的阻塞队列有ArrayBlockingQueue,LinkedBlockingDeque,LinkedBlockingQueue,这些队列有界且可以阻塞线程 ArrayBlockingQ ...

  8. Java并发基础框架AbstractQueuedSynchronizer初探(ReentrantLock的实现分析)

    AbstractQueuedSynchronizer是实现Java并发类库的一个基础框架,Java中的各种锁(RenentrantLock, ReentrantReadWriteLock)以及同步工具 ...

  9. java并发编程资料

    并发这玩意很有用,把自己在网上看过觉得总结的很好的资料分享出来.猛击下面的地址查看吧 java并发编程:线程池的使用说明 java并发编程系列文章 Java并发性和多线程专题 并发工具类 Java 7 ...

随机推荐

  1. mvn 搭建临时仓库批量下载依赖jar包

    1.新建文件夹temp,在temp下新建setup.bat ,pom.xml 2.编辑setup.bat 和pom.xml bsetup.bat call mvn -f pom.xml depende ...

  2. C++基础之字符串string

    C++基础之字符串string 标准库类型string表示可变长的字符序列,使用string类型必须首先包含string头文件.作为标准裤的一部分,string定义在命名空间std中. 定义和初始化s ...

  3. Python - 关于属性访问的优先级,属性访问顺序,python attributel lookup,类和实例访问属性的顺序

    object.__getattribute__(self, name) 类中的数据描述符 object.__dict__.get(name) 自身属性字典 object.__class__.__dic ...

  4. DotNet中静态成员、静态类、静态构造方法和实例构造方法的区别与联系

    在面向对象的C#程序设计中,关于静态的概念一直是很多人搞不明白的.下面介绍这些带“静态”的名称. 1.静态成员: 定义:静态成员是用static关键字修饰的成员(包括字段属性和方法) 所属:静态成员是 ...

  5. 使用Vue时localhost:8080中localhost换成ip地址后无法显示页面的问题

    解决办法是:在package.json中 然后重新启动服务器 npm run dev 就正常显示了.

  6. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  7. 【原】python总结

    python3浅拷贝和深拷贝:https://www.jianshu.com/p/c7e72fcad407

  8. C++11常用特性介绍——列表初始化

    一.列表初始化 1)C++11以前,定义初始化的几种不同形式,如下: int data = 0;   //赋值初始化 int data = {0};   //花括号初始化 int data(0); / ...

  9. FTP-Publisher Plugin

    使用 FTP-Publisher Plugin 在 http://wiki.hudson-ci.org/display/HUDSON/FTP-Publisher+Plugin 有详细说明, 需要注意的 ...

  10. Django 学习之Django Rest Framework(DRF)

    一. WEB应用模式 在开发Web应用中,有两种应用模式 1. 前后端不分离 把html模板文件和django的模板语法结合渲染完成以后才从服务器返回给客户. 2. 前后端分离 二. API接口 AP ...