23.读写锁ReadWriteLock

ReentrantReadWriteLock
    所谓的读写锁,是访问资源共享共享锁、互斥锁,如果对资源加了写锁,其他线程无法获取写锁与读锁,但是持有写锁的线程,可以对资源
    加读锁;如果一个线程对资源加了读锁,那么其他线程可以继续加读锁。读读共享、读写互斥、写写互斥
  1. package dmeo9;
  2. public class MyAccount {
  3. private String oid;
  4. private int cash;
  5. public MyAccount(){
  6. }
  7. public MyAccount(String oid, int cash) {
  8. this.oid = oid;
  9. this.cash = cash;
  10. }
  11. public String getOid() {
  12. return oid;
  13. }
  14. public void setOid(String oid) {
  15. this.oid = oid;
  16. }
  17. public int getCash() {
  18. return cash;
  19. }
  20. public void setCash(int cash) {
  21. this.cash = cash;
  22. }
  23. }
  1. package dmeo9;
  2. import java.util.concurrent.locks.ReadWriteLock;
  3. public class User implements Runnable {
  4. private String name;//用户名
  5. private MyAccount myAccount;//操作的账户
  6. private int ioCash;//操作的金额
  7. private ReadWriteLock readWriteLock;//读写锁操作锁需要的锁
  8. private boolean isCheck;//是否操作?
  9. public User() {
  10. }
  11. public User(String name, MyAccount myAccount, int ioCash, ReadWriteLock readWriteLock, boolean isCheck) {
  12. this.name = name;
  13. this.myAccount = myAccount;
  14. this.ioCash = ioCash;
  15. this.readWriteLock = readWriteLock;
  16. this.isCheck = isCheck;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public MyAccount getMyAccount() {
  25. return myAccount;
  26. }
  27. public void setMyAccount(MyAccount myAccount) {
  28. this.myAccount = myAccount;
  29. }
  30. public int getIoCash() {
  31. return ioCash;
  32. }
  33. public void setIoCash(int ioCash) {
  34. this.ioCash = ioCash;
  35. }
  36. public ReadWriteLock getReadWriteLock() {
  37. return readWriteLock;
  38. }
  39. public void setReadWriteLock(ReadWriteLock readWriteLock) {
  40. this.readWriteLock = readWriteLock;
  41. }
  42. public boolean isCheck() {
  43. return isCheck;
  44. }
  45. public void setCheck(boolean check) {
  46. isCheck = check;
  47. }
  48. @Override
  49. public void run() {
  50. if (isCheck) {
  51. /*获取读锁*/
  52. try {
  53. readWriteLock.readLock().lock();
  54. System.err.println("\n读:" + getName() + "\t正在read 查询\t" + getMyAccount() + "\t账户,当前金额为:" + myAccount.getCash());
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. } finally {
  58. /*释放读锁*/
  59. readWriteLock.readLock().unlock();
  60. }
  61. } else {
  62. /*获取写的锁*/
  63. try {
  64. readWriteLock.writeLock().lock();
  65. System.err.println("写:" + getName() + "\t正在write 操作\t" + getMyAccount() + "账户,当前金额为:" + myAccount.getCash());
  66. getMyAccount().setCash(getMyAccount().getCash() + getIoCash());
  67. System.err.println("操作成功:金额为" + getIoCash() + "\t当前金额为:" + getMyAccount().getCash()+"\n\n");
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. } finally {
  71. /*释放写锁*/
  72. /**
  73. * 一般用lock或者 readwritelock时,需要把
  74. unlock方法放在一个 fianlly 块中,因为程序运行的时候可能会
  75. 出现一些我们人为控制不了的因素,导致锁一直没释放,那其他线程就进不来了.
  76. */
  77. readWriteLock.writeLock().unlock();
  78. }
  79. }
  80. }
  81. }
  1. package dmeo9;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.locks.ReadWriteLock;
  5. import java.util.concurrent.locks.ReentrantReadWriteLock;
  6. public class DemoReadWriteLock {
  7. public static void main(String[] args){
  8. MyAccount myAccount = new MyAccount("工商银行95599200901215522",160000);
  9. ReadWriteLock readWriteLock = new ReentrantReadWriteLock(false);
  10. ExecutorService executorService = Executors.newFixedThreadPool(2);
  11. User u1 = new User("张三", myAccount, -4000, readWriteLock, false);
  12. User u2 = new User("小明", myAccount, 6000, readWriteLock, false);
  13. User u3 = new User("小明", myAccount, -8000, readWriteLock, false);
  14. User u4 = new User("小明", myAccount, 800, readWriteLock, false);
  15. User u5 = new User("小明他爹", myAccount, 0, readWriteLock, true);
  16. executorService.execute(u1);
  17. executorService.execute(u2);
  18. executorService.execute(u3);
  19. executorService.execute(u4);
  20. executorService.execute(u5);
  21. /*关闭线程池*/
  22. executorService.shutdown();
  23. }
  24. }
  25. 输出:
  26. 写:张三 正在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
posted @ 2017-08-10 02:06  逍遥叹!!  阅读(243)  评论(0编辑  收藏  举报