zoukankan      html  css  js  c++  java
  • java多线程----ReentrantReadWriteLock

    package com.test;
    import java.util.concurrent.locks.ReadWriteLock; 
    import java.util.concurrent.locks.ReentrantReadWriteLock; 
    
    public class ReadWriteLockTest1 { 
    
        public static void main(String[] args) { 
            MyCount myCount = new MyCount("4238920615242830", 10000); 
            User user = new User("Tommy", myCount); 
    
            for (int i=0; i<2; i++) {
                //只要有一个线程获取到写锁,则其他线程获取不到写锁和读锁。  只要有一个读锁没有释放,其他线程获取不到写锁。
                user.getCash();
                user.setCash((i+1)*1000);
            }
        } 
    } 
    
    class User {
        private String name;           
        private MyCount myCount;        
        private ReadWriteLock myLock; 
    
        User(String name, MyCount myCount) {
            this.name = name; 
            this.myCount = myCount; 
            this.myLock = new ReentrantReadWriteLock();
        }
    
        public void getCash() {
            new Thread() {
                public void run() {
                    myLock.readLock().lock(); 
                    try {
                        System.out.println(Thread.currentThread().getName() +" getCash start"); 
                        myCount.getCash();
                        Thread.sleep(1);
                        System.out.println(Thread.currentThread().getName() +" getCash end"); 
                    } catch (InterruptedException e) {
                    } finally {
                        myLock.readLock().unlock(); 
                    }
                }
            }.start();
        }
    
        public void setCash(final int cash) {
            new Thread() {
                public void run() {
                    myLock.writeLock().lock(); 
                    try {
                        System.out.println(Thread.currentThread().getName() +" setCash start"); 
                        myCount.setCash(cash);
                        Thread.sleep(1);
                        System.out.println(Thread.currentThread().getName() +" setCash end"); 
                    } catch (InterruptedException e) {
                    } finally {
                        myLock.writeLock().unlock(); 
                    }
                }
            }.start();
        }
    }
    
    class MyCount {
        private String id;        
        private int    cash;      
    
        MyCount(String id, int cash) { 
            this.id = id; 
            this.cash = cash; 
        } 
    
        public String getId() { 
            return id; 
        } 
    
        public void setId(String id) { 
            this.id = id; 
        } 
    
        public int getCash() { 
            System.out.println(Thread.currentThread().getName() +" getCash cash="+ cash); 
            return cash; 
        } 
    
        public void setCash(int cash) { 
            System.out.println(Thread.currentThread().getName() +" setCash cash="+ cash); 
            this.cash = cash; 
        } 
    }
    ReadWriteLock,顾名思义,是读写锁。它维护了一对相关的锁 — — “读取锁”和“写入锁”,一个用于读取操作,另一个用于写入操作。
    “读取锁”用于只读操作,它是“共享锁”,能同时被多个线程获取。
    “写入锁”用于写入操作,它是“独占锁”,写入锁只能被一个线程锁获取。
    注意:不能同时存在读取锁和写入锁!
    ReadWriteLock是一个接口。ReentrantReadWriteLock是它的实现类,ReentrantReadWriteLock包括子类ReadLock和WriteLock。
  • 相关阅读:
    旋转坐标转换的矩阵推导
    Union的妙用和注意
    OpenGL环境搭建Windows+Mac+Linux
    RichLabel基于Cocos2dx+Lua v3.x
    Cocos2dx+lua中Color参数的坑
    常见开源协议
    lua序列化(支持循环引用)
    lua标签解析器
    Markdown基本语法
    python3爬取墨迹天气并发送给微信好友,附源码
  • 原文地址:https://www.cnblogs.com/yaowen/p/9334370.html
Copyright © 2011-2022 走看看