重入锁ReentrantLock常用方法(三)

1. isHeldByCurrentThread()

    查询当前线程是否保持锁定。

    isLocked()

    查询是否存在任意线程保持此锁定。

public class ReentrantLockMethodTest7 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){
        try{
            System.out.println("isHeldByCurrentThread:" + lock.isHeldByCurrentThread());
            System.out.println("isLocked:" + lock.isLocked());
            lock.lock();
            System.out.println("isHeldByCurrentThread:" + lock.isHeldByCurrentThread());
            System.out.println("isLocked:" + lock.isLocked());
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){

        ReentrantLockMethodTest7 test7 = new ReentrantLockMethodTest7();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test7.testMethod();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

运行结果,

isHeldByCurrentThread,在使用重入锁进行锁定后,当前线程保持锁定状态。

isLocked,使用重入锁进行锁定后,存在一个线程保持此锁定。



2. lockInterruptbly()

    如果当前线程未被中断,则获取锁定;如果已中断,则抛出异常(InterruptedException)

public class ReentrantLockMethodTest8 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){

        try{
            lock.lockInterruptibly();
            for (int i = 0; i < 10; i++){
                System.out.println("Thread---" + Thread.currentThread().getName() + "保持锁定!");
            }
        } catch (InterruptedException e){
            System.out.println("Thread---" + Thread.currentThread().getName() + "异常!");
            e.printStackTrace();
        } finally{
            lock.unlock();
        }
    }

    public static void main(String[] args){
        ReentrantLockMethodTest8 test8 = new ReentrantLockMethodTest8();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test8.testMethod();
            }
        };
        // A线程正常运行
        Thread threadA = new Thread(runnable);
        threadA.setName("A");
        threadA.start();

        Thread threadB = new Thread(runnable);
        threadB.setName("B");
        threadB.start();
        // B线程标记中断
        threadB.interrupt();
    }
}

运行结果



3. tryLock()

    尝试获取锁定,如果锁定没有被别的线程保持,则获取锁定,即成功获取返回true,否则返回false(锁定已被别的线程获取)

    tryLock(long timeout, TimeUnit unit)

    尝试获取锁定,如果锁定没有被别的线程保持,则获取锁定,即成功获取返回true,如果没有获取锁定,则等待指定的时间,要是在指定时间内获取锁定,返回true,否则返回false。

public class ReentrantLockMethodTest9 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){
            if(lock.tryLock()){
                System.out.println("Thread---" + Thread.currentThread().getName()+"---获得锁");
            }else{
                System.out.println("Thread---" + Thread.currentThread().getName()+"---没有获得锁");
            }
        
        // 测试tryLock(long time, TimeUnit unit)
//        try{
//            if(lock.tryLock(1, TimeUnit.SECONDS)){
//                System.out.println("Thread---" + Thread.currentThread().getName()+"---获得锁");
//            }else{
//                System.out.println("Thread---" + Thread.currentThread().getName()+"---没有获得锁");
//            }
//        } catch (InterruptedException e){
//            e.printStackTrace();
//        } finally {
//            lock.unlock();
//        }

    }

    public static void main(String[] args){
        ReentrantLockMethodTest9 test9 = new ReentrantLockMethodTest9();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test9.testMethod();
            }
        };

        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.setName("A");
        threadB.setName("B");
        threadA.start();
        threadB.start();
    }
}

运行结果,A获取锁返回true,B没有获得锁返回false


将测试tryLock(long time, TimeUnit unit)的代码注释去掉并注释掉tryLock()方法

运行结果,A获取锁定,执行完后解锁,B线程在指定的时间内获取了锁。

    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值