ReentrantLock示例

在这里插入图片描述

  1. ReentrantLock的可重入性
public class ReentrantLockTest01 {

    public static void main(String[] args) {
        //默认为非公平锁
        ReentrantLock lock = new ReentrantLock();

        //在同一线程中,ReentrantLock锁可重入(可重入:同一线程可以多次获取同一个锁)
        for (int i = 0; i < 3; i++) {
            System.out.println("lock");
            lock.lock();
            System.out.println("hold count is: " + lock.getHoldCount());
        }

        for (int j = 0; j < 3; j++) {
            System.out.println("unlock");
            //注意lock和unlock必须要成对出现,一般unlock会放在finally中
            lock.unlock();
        }
    }
}
  1. 多线程场景
public class ReentrantLockTest02 {

    //默认为非公平锁
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new MyThread()).start();
        }
    }

    static class MyThread implements Runnable {

        public void run() {
            System.out.println(Thread.currentThread().getName() + " 加锁");
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName() + " 执行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName() + " 释放锁");
                lock.unlock();
            }
        }
    }
}
  1. ReentrantLock的可中断性
public class ReentrantLockTest03 {

    //默认为非公平锁
    private static ReentrantLock lock01 = new ReentrantLock(false);
    private static ReentrantLock lock02 = new ReentrantLock(false);

    public static void main(String[] args) {
        //创建死锁场景
        Thread thread01 = new Thread(new MyThread(lock01,lock02));
        Thread thread02 = new Thread(new MyThread(lock02,lock01));

        thread01.start();
        thread02.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //中断其中一个线程
        thread01.interrupt();
    }

    static class MyThread implements Runnable {

        Lock firstLock;
        Lock secondLock;

        MyThread(Lock firstLock,Lock secondLock){
            this.firstLock = firstLock;
            this.secondLock = secondLock;
        }

        public void run() {
            try {
                //锁的可中断处理
                firstLock.lockInterruptibly();
                System.out.println(Thread.currentThread().getName()+" 获取锁:"+firstLock.toString());
                TimeUnit.SECONDS.sleep(2);
                secondLock.lockInterruptibly();
                System.out.println(Thread.currentThread().getName()+" 获取锁:"+secondLock.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                //注意释放锁
                firstLock.unlock();
                secondLock.unlock();
                System.out.println(Thread.currentThread().getName()+" 处理完毕");
            }
        }
    }
}

参考:

  1. https://www.cnblogs.com/gxyandwmm/p/9387833.html
  2. https://www.cnblogs.com/takumicx/p/9338983.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值