zoukankan      html  css  js  c++  java
  • 自嗨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();
        }
    
    }
  • 相关阅读:
    最近工作状态异常的原因追寻。
    当“逻辑”与“UE”冲突时
    面对一个“丢失了与用户“签订”的协议的修改”时进行的思考。
    如果公司的需求总是让研发部门有怨言…
    安装sybase12.0,运行时报错异常。
    你看到这份文档,我就想摔鼠标!
    对于研发组长的责任产生了疑惑。
    关于html中空格导致的排版问题
    如何配置你的工作环境。
    今天的笔记:2014年6月3日
  • 原文地址:https://www.cnblogs.com/hikoukay/p/14166625.html
Copyright © 2011-2022 走看看