美文网首页
J.U.C-AQS-ReentrantLock

J.U.C-AQS-ReentrantLock

作者: 墨平语凡 | 来源:发表于2018-06-03 12:06 被阅读0次

    ReentrantLock(可重入锁)和synchronized区别

    • 可重入性
    • 锁的实现(ReentrantLock是JDK实现,synchronized是依赖于JVM实现,前者可以阅读源码个人实现)
    • 性能的区别(目前差不多,倾向于synchronized)
    • 功能的区别
    function.png

    当需要使用ReentrantLock的独有功能时使用该锁

    package io.haitaoc.concurrency.example.lock;
    
    
    
    import io.haitaoc.concurrency.annotation.ThreadSafe;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    @ThreadSafe
    public class LockExample2 {
    
        // 请求总数
        public static int clientTotal = 5000;
    
        // 同时并发执行的线程数
        public static int threadTotal = 200;
    
        public static int count = 0;
    
        private final static Lock lock = new ReentrantLock();
    
        public static void main(String[] args) throws Exception {
            ExecutorService executorService = Executors.newCachedThreadPool();
            final Semaphore semaphore = new Semaphore(threadTotal);
            final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
            for (int i = 0; i < clientTotal ; i++) {
                executorService.execute(() -> {
                    try {
                        semaphore.acquire();
                        add();
                        semaphore.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                });
            }
            countDownLatch.await();
            executorService.shutdown();
            System.out.println("count: "+ count);
        }
    
        private static void add() {
            lock.lock();
            try {
                count++;
            } finally {
                lock.unlock();
            }
        }
    }
    
    
    package io.haitaoc.concurrency.example.lock;
    
    
    
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    
    
    /**
     * 悲观读取,执行写入操作坚决不允许进行读取操作
     * 当读多写少时,就会使写操作进入饥饿,一直进入阻塞状态
     */
    public class LockExample3 {
    
        private final Map<String, Data> map = new TreeMap<>();
    
        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    
        private final Lock readLock = lock.readLock();
    
        private final Lock writeLock = lock.writeLock();
    
        public Data get(String key) {
            readLock.lock();
            try {
                return map.get(key);
            } finally {
                readLock.unlock();
            }
        }
    
        public Set<String> getAllKeys() {
            readLock.lock();
            try {
                return map.keySet();
            } finally {
                readLock.unlock();
            }
        }
    
        public Data put(String key, Data value) {
            writeLock.lock();
            try {
                return map.put(key, value);
            } finally {
                readLock.unlock();
            }
        }
    
        class Data {
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:J.U.C-AQS-ReentrantLock

          本文链接:https://www.haomeiwen.com/subject/wvkjsftx.html