• 自嗨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();
        }
    
    }
  • 相关阅读:
    框架和库的区别
    象棋中常用的最小值最大值算法及剪枝算法
    cocos2dx 3.x中的渲染机制
    用vs2013编译lua源码方法
    VS中设置#define _CRT_SECURE_NO_WARNINGS的两种方式
    lua编程基础
    VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局
    (二)识别NAND Flash Nor Flash
    Linux设备驱动之Ioctl控制
    linux驱动程序中的并发控制
  • 原文地址:https://www.cnblogs.com/hikoukay/p/14166625.html
Copyright © 2020-2023  润新知