ReentrantLock可重入锁

public class ReentrantLockTest implements Runnable {
    public static ReentrantLock lock = new ReentrantLock();
    public static int i = 0;

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "启动!");
        lock.lock();  // 看这里就可以
        System.out.println(Thread.currentThread().getName() + "获取锁:" + lock.getHoldCount());
        try {
            i++;
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock(); // 看这里就可以
            System.out.println(Thread.currentThread().getName() + "释放锁" + lock.getHoldCount());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockTest test = new ReentrantLockTest();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);
        t1.start();
        t2.start();
        t1.join();
        t2.join(); // main线程会等待t1和t2都运行完再执行以后的流程
        System.err.println(i);
    }
}

在这里插入图片描述

lock()获取锁

  • 如果没有其它线程持有此锁,当前线程获取锁,holdCount = 1,立即返回
  • 如果当前线程已经持有此锁,holdCount++,立即返回
  • 如果其它线程持锁,当前线程挂起等待线程调度,获取到锁,holdCount = 1
public void lock() {
        sync.lock();
}

lockInterruptibly() 获取锁,直到当前线程被中断

  • 如果没有其它线程持有此锁,当前线程获取锁,holdCount = 1,立即返回

  • 如果当前线程已经持有此锁,holdCount++,立即返回

  • 如果其它线程持锁,当前线程挂起等待线程调度,

      条件1:获取到锁,holdCount = 1
      条件2:其它线程中断了当前线程,抛出一个InterruptedException
    
     *
     * <p>In this implementation, as this method is an explicit
     * interruption point, preference is given to responding to the
     * interrupt over normal or reentrant acquisition of the lock.
  
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }
  • lock方法会忽略中断请求,继续获取锁直到成功,才响应中断;

  • lockInterruptibly则直接抛出中断异常来立即响应中断,由上层调用者处理中断。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值