23.读写锁ReadWriteLock
ReentrantReadWriteLock
所谓的读写锁,是访问资源共享共享锁、互斥锁,如果对资源加了写锁,其他线程无法获取写锁与读锁,但是持有写锁的线程,可以对资源
加读锁;如果一个线程对资源加了读锁,那么其他线程可以继续加读锁。读读共享、读写互斥、写写互斥
package dmeo9;public class MyAccount {private String oid;private int cash;public MyAccount(){}public MyAccount(String oid, int cash) {this.oid = oid;this.cash = cash;}public String getOid() {return oid;}public void setOid(String oid) {this.oid = oid;}public int getCash() {return cash;}public void setCash(int cash) {this.cash = cash;}}
package dmeo9;import java.util.concurrent.locks.ReadWriteLock;public class User implements Runnable {private String name;//用户名private MyAccount myAccount;//操作的账户private int ioCash;//操作的金额private ReadWriteLock readWriteLock;//读写锁操作锁需要的锁private boolean isCheck;//是否操作?public User() {}public User(String name, MyAccount myAccount, int ioCash, ReadWriteLock readWriteLock, boolean isCheck) {this.name = name;this.myAccount = myAccount;this.ioCash = ioCash;this.readWriteLock = readWriteLock;this.isCheck = isCheck;}public String getName() {return name;}public void setName(String name) {this.name = name;}public MyAccount getMyAccount() {return myAccount;}public void setMyAccount(MyAccount myAccount) {this.myAccount = myAccount;}public int getIoCash() {return ioCash;}public void setIoCash(int ioCash) {this.ioCash = ioCash;}public ReadWriteLock getReadWriteLock() {return readWriteLock;}public void setReadWriteLock(ReadWriteLock readWriteLock) {this.readWriteLock = readWriteLock;}public boolean isCheck() {return isCheck;}public void setCheck(boolean check) {isCheck = check;}@Overridepublic void run() {if (isCheck) {/*获取读锁*/try {readWriteLock.readLock().lock();System.err.println("\n读:" + getName() + "\t正在read 查询\t" + getMyAccount() + "\t账户,当前金额为:" + myAccount.getCash());} catch (Exception e) {e.printStackTrace();} finally {/*释放读锁*/readWriteLock.readLock().unlock();}} else {/*获取写的锁*/try {readWriteLock.writeLock().lock();System.err.println("写:" + getName() + "\t正在write 操作\t" + getMyAccount() + "账户,当前金额为:" + myAccount.getCash());getMyAccount().setCash(getMyAccount().getCash() + getIoCash());System.err.println("操作成功:金额为" + getIoCash() + "\t当前金额为:" + getMyAccount().getCash()+"\n\n");} catch (Exception e) {e.printStackTrace();} finally {/*释放写锁*//*** 一般用lock或者 readwritelock时,需要把unlock方法放在一个 fianlly 块中,因为程序运行的时候可能会出现一些我们人为控制不了的因素,导致锁一直没释放,那其他线程就进不来了.*/readWriteLock.writeLock().unlock();}}}}
package dmeo9;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class DemoReadWriteLock {public static void main(String[] args){MyAccount myAccount = new MyAccount("工商银行95599200901215522",160000);ReadWriteLock readWriteLock = new ReentrantReadWriteLock(false);ExecutorService executorService = Executors.newFixedThreadPool(2);User u1 = new User("张三", myAccount, -4000, readWriteLock, false);User u2 = new User("小明", myAccount, 6000, readWriteLock, false);User u3 = new User("小明", myAccount, -8000, readWriteLock, false);User u4 = new User("小明", myAccount, 800, readWriteLock, false);User u5 = new User("小明他爹", myAccount, 0, readWriteLock, true);executorService.execute(u1);executorService.execute(u2);executorService.execute(u3);executorService.execute(u4);executorService.execute(u5);/*关闭线程池*/executorService.shutdown();}}输出:写:张三 正在write 操作 dmeo9.MyAccount@1a8fdcaa账户,当前金额为:160000
操作成功:金额为-4000 当前金额为:156000 写:小明 正在write 操作 dmeo9.MyAccount@1a8fdcaa账户,当前金额为:156000
操作成功:金额为6000 当前金额为:162000 写:小明 正在write 操作 dmeo9.MyAccount@1a8fdcaa账户,当前金额为:162000
操作成功:金额为-8000 当前金额为:154000 写:小明 正在write 操作 dmeo9.MyAccount@1a8fdcaa账户,当前金额为:154000
操作成功:金额为800 当前金额为:154800 读:小明他爹 正在read 查询 dmeo9.MyAccount@1a8fdcaa 账户,当前金额为:154800
23.读写锁ReadWriteLock的更多相关文章
- 线程中的读写锁ReadWriteLock
Lock锁还有两个非常强大的类 ReadWriteLock接口实现类ReentrantReadWriteLock(非常重要的锁) 想实现 读取的时候允许多线程并发访问,写入的时候不允许. 这种效果.. ...
- 显式锁(三)读写锁ReadWriteLock
前言: 上一篇文章,已经很详细地介绍了 显式锁Lock 以及 其常用的实现方式- - ReetrantLock(重入锁),本文将介绍另一种显式锁 - - 读写锁ReadWriteLock. ...
- 【漫画】互斥锁ReentrantLock不好用?试试读写锁ReadWriteLock
ReentrantLock完美实现了互斥,完美解决了并发问题.但是却意外发现它对于读多写少的场景效率实在不行.此时ReentrantReadWriteLock来救场了!一种适用于读多写少场景的锁,可以 ...
- 【漫画】读写锁ReadWriteLock还是不够快?再试试StampedLock!
本文来源于公众号[胖滚猪学编程] 转载请注明出处! 在互斥锁ReentrantLock不好用?试试读写锁ReadWriteLock一文中,我们对比了互斥锁ReentrantLock和读写锁ReadWr ...
- 读-写锁 ReadWriteLock & 线程八锁
读-写锁 ReadWriteLock: ①ReadWriteLock 维护了一对相关的锁,一个用于只读操作, 另一个用于写入操作. 只要没有 writer,读取锁可以由 多个 reader 线程同时保 ...
- C# 多线程编程之锁的使用【互斥锁(lock)和读写锁(ReadWriteLock)】
多线程编程之锁的使用[互斥锁(lock)和读写锁(ReadWriteLock)] http://blog.csdn.net/sqqyq/article/details/18651335 多线程程序写日 ...
- Java 读写锁 ReadWriteLock 原理与应用场景详解
Java并发编程提供了读写锁,主要用于读多写少的场景,今天我就重点来讲解读写锁的底层实现原理@mikechen 什么是读写锁? 读写锁并不是JAVA所特有的读写锁(Readers-Writer Loc ...
- 深入理解读写锁—ReadWriteLock源码分析
转载:https://blog.csdn.net/qq_19431333/article/details/70568478 ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁.读锁可以在 ...
- 读写锁ReadWriteLock和缓存实例
读写锁:多个读锁不互斥,读锁与写锁互斥,写锁与写锁互斥.即:读的时候不允许写,写的时候不允许读,可以同时读. synchronized关键字和普通的Lock构造的锁,会造成读与读之间的互斥, ...
随机推荐
- 【51nod】1709 复杂度分析
题解 考虑朴素的暴力,相当于枚举u点的每个祖先f,然后统计一下这个点f除了某个儿子里有u的那个子树之外的节点个数,乘上f到u距离的二进制1的个数 那么我们用倍增来实现这个东西,每次枚举二进制的最高位j ...
- mongdb 拓展的下载地址和编译安装(php)
下载地址:https://pecl.php.net/package/mongodb 编译安装: $ tar zxvf mongodb-mongodb-php-driver-<commit_id& ...
- 监控cpu、内存 <shell>
获取cpu.内存结果 pid=$1 #获取进程pid echo $pid interval=1 #设置采集间隔 while true do echo $(date +"%y-%m-%d %H ...
- [leetcode tree]96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- sublime3176注册码破解汉化及常用插件
官方网站下载地址:https://www.sublimetext.com/3 破解软件下载地址:https://www.lanzous.com/i1a7zfi 破解软件下载地址备用:https://d ...
- 直接插入排序(高级版)之C++实现
直接插入排序(高级版)之C++实现 一.源代码:InsertSortHigh.cpp /*直接插入排序思想: 假设待排序的记录存放在数组R[1..n]中.初始时,R[1]自成1个有序区,无序区为R[2 ...
- poj3537 Crosses and Crosses 博弈论
大意: 给定一个\(1 * n\)的棋盘,你和对手轮流在上面画"X" 当出现三个连续的X时,最后一步操作的人胜利 不难发现,在棋盘中画了一个X之后 问题等价于两个一样的子游戏 然后 ...
- 2018haoi总结
AM T1 写了40分,有50分的暴力分,只看到n=1能用费马小定理,没看到还有一个mod质数也能用费马小定理的10分. AM T2 写了10分,60分异或方程求自由元. AM T3 现在还没搞清楚题 ...
- 洛谷.4383.[八省联考2018]林克卡特树lct(树形DP 带权二分)
题目链接 \(Description\) 给定一棵边带权的树.求删掉K条边.再连上K条权为0的边后,新树的最大直径. \(n,K\leq3\times10^5\). \(Solution\) 题目可以 ...
- [LeetCode] Max Points on a Line 题解
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...