ReentrantLock

     创建一个显示锁

    复制代码
    package com.dwz.locks;
    
    import java.util.Optional;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.stream.IntStream;
    
    public class ReentrantLockExample {
        private static final Lock lock = new ReentrantLock();
        
        public static void main(String[] args) throws InterruptedException {
            IntStream.range(0, 2).forEach(i -> new Thread() {
                @Override
                public void run() {
                    needLock();
                }
            }.start());
        }
        
        public static void needLock() {
            try {
                lock.lock();
                Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    复制代码
    复制代码
        //needLock等价于needLockBySync
        public static void needLockBySync() {
            synchronized(ReentrantLockExample.class) {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    复制代码

    测试锁可以被打断:lock.lockInterruptibly();

    复制代码
    package com.dwz.locks;
    
    import java.util.Optional;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.stream.IntStream;
    
    public class ReentrantLockExample {
        private static final Lock lock = new ReentrantLock();
        
        public static void main(String[] args) throws InterruptedException {
            
            Thread thread1 = new Thread(() -> testUnInterruptibly());
            thread1.start();
            
            TimeUnit.SECONDS.sleep(1);
            
            Thread thread2 = new Thread(() -> testUnInterruptibly());
            thread2.start();
            
            TimeUnit.SECONDS.sleep(1);
            
            thread2.interrupt();
            System.out.println("=================");
    
        }
        public static void testUnInterruptibly() {
            try {
                lock.lockInterruptibly();
                Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
                while(true) {
                    
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    复制代码

    拿不到锁就算了:lock.tryLock()

    复制代码
    package com.dwz.locks;
    
    import java.util.Optional;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.stream.IntStream;
    
    public class ReentrantLockExample {
        private static final Lock lock = new ReentrantLock();
        
        public static void main(String[] args) throws InterruptedException {
            Thread thread1 = new Thread(() -> testTryLock());
            thread1.start();
            
            TimeUnit.SECONDS.sleep(1);
            
            Thread thread2 = new Thread(() -> testTryLock());
            thread2.start();
        }
        
        //拿不到锁就算了
        public static void testTryLock() {
            if(lock.tryLock()) {
                try {
                    Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
                    while(true) {
                        
                    }
                } finally {
                    lock.unlock();
                }
            } else {
                Optional.of("The thread-" + Thread.currentThread().getName() + " not get lock.").ifPresent(System.out::println);
            }
        }
    }
    复制代码

     测试ReentrantLock的getQueueLength()、hasQueuedThreads()、getHoldCount()方法

    复制代码
    package com.dwz.locks;
    
    import java.util.Optional;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ReentrantLockExample2 {
        private static final ReentrantLock lock = new ReentrantLock();
        
        public static void main(String[] args) throws InterruptedException {
            Thread thread1 = new Thread(() -> testUnInterruptibly());
            thread1.start();
            
            TimeUnit.SECONDS.sleep(1);
            
            Thread thread2 = new Thread(() -> testUnInterruptibly());
            thread2.start();
            
            TimeUnit.SECONDS.sleep(1);
            Optional.of(lock.getQueueLength()).ifPresent(System.out::println);
            Optional.of(lock.hasQueuedThreads()).ifPresent(System.out::println);
        }
        
        public static void testUnInterruptibly() {
            try {
                lock.lockInterruptibly();
                Optional.of(Thread.currentThread().getName() + ":" + lock.getHoldCount()).ifPresent(System.out::println);
                Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
                while(true) {
                    
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    复制代码

     

    posted @   龙宇在天  阅读(169)  评论(0编辑  收藏  举报
    //右侧添加目录 //增加页面点击显示24字社会主义核心价值观
    点击右上角即可分享
    微信分享提示