• 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();
            }
        }
    }
  • 相关阅读:
    jieba库分词统计
    第九次作业——测试报告和用户手册
    第八次作业——系统设计和任务分配
    第七次作业-团队选题报告和需求规格说明书
    第六次作业——结对项目之需求分析与原型设计
    小学四则运算的简单实现
    jieba库分词
    第九次团队作业——测试报告和用户手册
    第八次作业——系统设计与团队分配(个人)
    团队项目之选题报告和需求规格说明书
  • 原文地址:https://www.cnblogs.com/zheaven/p/13360130.html
Copyright © 2020-2023  润新知