• 自嗨ReentrantReadWriteLock


    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    
    public class AReentrantReadWriteLock {
        static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        public static void main(String[] args) throws InterruptedException {
    //        deadLock();
    //        releaseLock();
    //        upGradLock();
    //        downGrad();
    //        testSync();
    //        testRWL();
    //        readWriteSameTime();
    //        writeWriteSameTime();
            readReadSameTime();
        }
    
        /**
         * 同时读测试
         */
        public static void readReadSameTime(){
            ExecutorService service= Executors.newCachedThreadPool();
            service.execute(new Runnable() {
                @Override
                public void run() {
                    readFile(Thread.currentThread());
                }
            });
            service.execute(new Runnable() {
                @Override
                public void run() {
                    readFile(Thread.currentThread());
                }
            });
        }
        /**
         * 同时写测试
         */
        public static void writeWriteSameTime(){
            ExecutorService service= Executors.newCachedThreadPool();
            service.execute(new Runnable() {
                @Override
                public void run() {
                    writeFile(Thread.currentThread());
                }
            });
            service.execute(new Runnable() {
                @Override
                public void run() {
                    writeFile(Thread.currentThread());
                }
            });
        }
        /**
         * 同时读写测试
         */
        public static void readWriteSameTime(){
            ExecutorService service= Executors.newCachedThreadPool();
            service.execute(new Runnable() {
                @Override
                public void run() {
                    writeFile(Thread.currentThread());
                }
            });
            service.execute(new Runnable() {
                @Override
                public void run() {
                    readFile(Thread.currentThread());
                }
            });
        }
        // 读操作
        public static void readFile(Thread thread) {
            lock.readLock().lock();
            boolean readLock = lock.isWriteLocked();
            if (!readLock) {
                System.out.println("当前为读锁!");
            }
            try {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(thread.getName() + ":正在进行读操作……");
                }
                System.out.println(thread.getName() + ":读操作完毕!");
            } finally {
                System.out.println("释放读锁!");
                lock.readLock().unlock();
            }
        }
    
        // 写操作
        public static void writeFile(Thread thread) {
            lock.writeLock().lock();
            boolean writeLock = lock.isWriteLocked();
            if (writeLock) {
                System.out.println("当前为写锁!");
            }
            try {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(thread.getName() + ":正在进行写操作……");
                }
                System.out.println(thread.getName() + ":写操作完毕!");
            } finally {
                System.out.println("释放写锁!");
                lock.writeLock().unlock();
            }
        }
        /**
         * 测试读写锁同时执行
         */
        public static void testRWL(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getRWL(Thread.currentThread());
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getRWL(Thread.currentThread());
                }
            }).start();
        }
    
        /**
         * ReentrantReadWriteLock
         * @param thread
         */
        public static void getRWL(Thread thread) {
            lock.readLock().lock();
            System.out.println("start time:" + System.currentTimeMillis());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(thread.getName() + ":正在进行读操作……");
            }
            System.out.println(thread.getName() + ":读操作完毕!");
            System.out.println("end time:" + System.currentTimeMillis());
            lock.readLock().unlock();
        }
    
        public static void testSync(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getSync(Thread.currentThread());
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getSync(Thread.currentThread());
                }
            }).start();
        }
        /**
         * synchronized实现读写锁
         * @param thread
         */
        public synchronized static void getSync(Thread thread) {
            System.out.println("start time:" + System.currentTimeMillis());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(thread.getName() + ":正在进行读操作……");
            }
            System.out.println(thread.getName() + ":读操作完毕!");
            System.out.println("end time:" + System.currentTimeMillis());
        }
    
        /**
         * 锁降级
         * ReentrantReadWriteLock支持锁降级
         */
        public static void downGrad(){
            ReentrantReadWriteLock rtLock = new ReentrantReadWriteLock();
            rtLock.writeLock().lock();
            System.out.println("writeLock");
    
            rtLock.readLock().lock();
            System.out.println("get read lock");
        }
        /**
         * 锁升级
         * ReentrantReadWriteLock不支持锁升级
         */
        public static void upGradLock(){
            ReentrantReadWriteLock rtLock = new ReentrantReadWriteLock();
            rtLock.readLock().lock();
            System.out.println("get readLock.");
            rtLock.writeLock().lock();
            System.out.println("blocking");
        }
        /**
         * 死锁
         * 获得几次锁,就要释放几次锁
         * @throws InterruptedException
         */
        public static void deadLock() throws InterruptedException {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.writeLock().lock();
                    System.out.println("Thread real execute");
                    lock.writeLock().unlock();
                }
            });
    
            lock.writeLock().lock();
            lock.writeLock().lock();
            t.start();
            Thread.sleep(200);
    
            System.out.println("realse one once");
            lock.writeLock().unlock();
        }
    
        /**
         * 解锁
         * 获得几次锁,就要释放几次锁
         * @throws InterruptedException
         */
        public static void releaseLock() throws InterruptedException {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.writeLock().lock();
                    System.out.println("Thread real execute");
                    lock.writeLock().unlock();
                }
            });
    
            lock.writeLock().lock();
            lock.writeLock().lock();
            t.start();
            Thread.sleep(200);
    
            System.out.println("realse one once");
            lock.writeLock().unlock();
            lock.writeLock().unlock();
        }
    
    }
  • 相关阅读:
    Linux如何开启mysql版本
    解决Centos7安装nginx后提示“Welcome to nginx on Fedora!”,conf.d目录下无default.conf文件
    如何屏蔽root用户(禁止以root用户登陆)的方法
    Linux修改文件时候,产生了一个.swap交换文件,如何删除
    Linux私钥公钥对认证身份,实现免密登陆
    Linux7/Centos7破解root用户密码
    Linux常用命令
    oracle 触发器 dblink
    phoenix sql语句优化
    如何在服务器端获取客户端的数据
  • 原文地址:https://www.cnblogs.com/hikoukay/p/14166625.html
Copyright © 2020-2023  润新知