ReentrantLock的阻塞性、可中断性

结论:

  • lock() 如果没有获取到锁,会一直阻塞并尝试获取锁,直到获取到锁。
  • lock() 获取到锁之前,其他线程不可以中断该线程。因为线程Thread如线程t2的interrupt方法,想要中断线程,但不会真的中断,只会把t2的中断标志改变,所以线程t2还会继续运行。
  • lockInterruptibly() 获取到锁之前,其他线程可以中断该线程。因为对interrupt标志进行了处理

 

lock()阻塞获取锁。如果获取不到锁,会一直阻塞,直到获取到锁

代码:

    static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        // t1先获取锁
        Thread t1 = new Thread(()->{
            try {
                reentrantLock.lock();
                System.out.println("t1获取到锁");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("t1报错");
                    throw new RuntimeException(e);
                }
            } finally {
                reentrantLock.unlock();
            }

            System.out.println("11释放锁");
        });
        System.out.println("t1 name :"+t1.getName());
        t1.start();
        // 测试重点。t2后获取锁。测试lock、lockInterruptibly方法
        Thread t2 = new Thread(()->{
            try {
                // 确保t先执行性
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("t2报错");
                throw new RuntimeException(e);
            }
            try {
                long l = System.currentTimeMillis();
                reentrantLock.lock();
                long l1 = System.currentTimeMillis();
                System.out.println("t2等待获取锁的时间"+(l1-l));
            } finally {
                reentrantLock.unlock();
            }
        });
        System.out.println("t2 name :"+t2.getName());
        t2.start();
}

结果:

697a090a5bb245e8b31063117b27c9e3.png

 

lockInterruptibly()。获取锁前该线程可以被中断。

代码:在上面代码的基础上,lock换成lockInterruptibly,加了个t3线程(对t2进行中断操作)

    static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        // t1先获取锁
        Thread t1 = new Thread(()->{
            try {
                reentrantLock.lock();
                System.out.println("t1获取到锁");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("t1报错");
                    throw new RuntimeException(e);
                }
            } finally {
                reentrantLock.unlock();
            }

            System.out.println("11释放锁");
        });
        System.out.println("t1 name :"+t1.getName());
        t1.start();

        // 测试重点。t2后获取锁。测试lock、lockInterruptibly方法
        Thread t2 = new Thread(()->{
            try {
                // 确保t先执行性
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("t2报错");
                throw new RuntimeException(e);
            }
            try {
                long l = System.currentTimeMillis();
                // reentrantLock.lock();
                reentrantLock.lockInterruptibly();
                long l1 = System.currentTimeMillis();
                System.out.println("t2等待获取锁的时间"+(l1-l));
            } catch (InterruptedException e) {
                System.out.println("t2获取锁失败,t2需要中断,t2要退出啦");
                throw new RuntimeException(e);
            } finally {
                reentrantLock.unlock();
            }
        });
        System.out.println("t2 name :"+t2.getName());
        t2.start();

        // 用来中断t2的
        Thread t3 = new Thread(()->{
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                System.out.println("t3报错");
                throw new RuntimeException(e);
            }
            t2.interrupt();
            System.out.println("t3中断t2了");
        });
        System.out.println("t3 name :"+t3.getName());
        t3.start();
    }

结果:3d4e52105f6149bd81ca7094c64d2019.png

 

如果t2没用lockInterruptibly(),而是用lock()。t3中断t2是中断不了的,因为interrupt方法只是打个标记,不会真的去中断方法。lock()针对这种打标记没做处理,所以t2会继续运行

结果如下:

e6fb2ae29fdc4113b9fe524ff938d44d.png

 

 

 

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值