JAVA进步一点点--JAVA基础知识--ReentranLock

参考黑马程序猿 并发教程
https://www.bilibili.com/video/BV16J411h7Rd?p=120
特点

  • 可中段
  • 可以设置超时时间
  • 可以设置为公平锁
  • 支持多个条件变量
    与sychronized一样,都支持可重入
    基本用法

关注:lock必须与unlock成对出现,unlock必须放在finally的最后,lock后紧跟try在try外部和里面是一样的

    //锁是静态不可变对象,保证了只有一个锁
    private static final ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {
        //上锁和解锁是成对的,解锁必须放在finally的最后
        reentrantLock.lock();
        try {
            System.out.println("hello");
            throw new IOException("can find file");
        } catch (IOException e) {
            System.out.println("service not avaliable");
        } finally {
            reentrantLock.unlock();
        }
    }

可重入
https://blog.csdn.net/u012545728/article/details/80843595

可重入:同一个线程如果首次获得了锁,再次获得锁时是可以直接进入的
不可重入:同一个线程如果首次获得了锁,再次获得锁时是会阻塞的

实例,m1是重新加锁,output如下,可以看到确实是重新进入了。
hello
m1 lock
service not avaliable

    //锁是静态不可变对象,保证了只有一个锁
    private static final ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {
        //上锁和解锁是成对的,解锁必须放在finally的最后
        reentrantLock.lock();
        try {
            System.out.println("hello");
            m1();
            throw new IOException("can find file");
        } catch (IOException e) {
            System.out.println("service not avaliable");
        } finally {
            reentrantLock.unlock();
        }
    }

    public static void m1() {
        reentrantLock.lock();
        try {
            System.out.println("m1 lock");
            throw new IOException();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }

可打断

输出如下:
打断Thread1的等待
t1 is interrupted
thread is interptued
可以看到打断后返回了,然后执行了后面的,如果打断后不返回,就会继续执行下面的 thread get lock,

      //锁是静态不可变对象,保证了只有一个锁
    private static final ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {

        reentrantLock.lock();


        Thread t1 = new Thread(()->{
            try {
                //如果有晶振
                reentrantLock.lockInterruptibly();
            }catch (InterruptedException e) {
                System.out.println("thread is interptued");
                return;
            }
            try{
                System.out.println("thread1 get lock");
                throw new IOException();
            }catch (IOException e){

            }
            finally {
                reentrantLock.unlock();
            }
        },"thread1");

        t1.start();
        System.out.println("打断Thread1的等待");
        Thread.sleep(5);
        t1.interrupt();
        System.out.println("t1 is interrupted");

    }

设置等待时间

    private static final ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {

        reentrantLock.lock();
        Thread t1 = new Thread(() -> {
            try {
            //此处可以带参数,也可以不带参数,返回时一个boolean
                if (!reentrantLock.tryLock(5, TimeUnit.SECONDS)) {
                    System.out.println("thread1 not get lcck");
                    return;
                }
            } catch (InterruptedException e) {
                System.out.println("thread1 not get lcck");
                e.printStackTrace();
            }
            try {
                System.out.println("thread get lock");
            } finally {
                reentrantLock.unlock();
            }
        }, "thread1");

        t1.start();
        Thread.sleep(2);
        System.out.println("main after 1s unlock");
        reentrantLock.unlock();
    }

公平锁
ReentrantLock默认是不公平锁,构造函数设置成true时,会是公平锁

private static final ReentrantLock reentrantLock = new ReentrantLock(true);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值