重入锁ReentrantLock常用方法(一)

        重入锁ReentrantLock常用方法

        1. int getHoldCount() 

            Queries the number of holds on this lock by the current thread.

            查询当前线程保持此锁定的个数,即调用lock()方法的次数。

public class ReentrantLockMethodTest1 {

    private ReentrantLock reentrantLock = new ReentrantLock();

    public void test1(){
        try{
            reentrantLock.lock();
            System.out.println("Lock count---" + reentrantLock.getHoldCount());
            test2();
        } finally {
            reentrantLock.unlock();
        }
    }

    public void test2(){

        try{
            reentrantLock.lock();
            System.out.println("Lock count---" + reentrantLock.getHoldCount());
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args){

        ReentrantLockMethodTest1 reentrantLockMethodTest1 = new ReentrantLockMethodTest1();
        reentrantLockMethodTest1.test1();
    }
}

运行结果



        2. int getQueueLength()

            Returns an estimate of the number of threads waiting to acquire this lock.

            返回正等待获取此锁定的预估线程数。

public class ReentrantLockMethodTest2 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){

        try{
            lock.lock();
            System.out.println("Thread---" + Thread.currentThread().getName() + "---Get Lock");
            Thread.sleep(1000);
            System.out.println("预估等待锁线程数:" + lock.getQueueLength());
        } catch (InterruptedException e){
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

测试类

public class ReentrantLockTest {

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

        ReentrantLockMethodTest2 test2 = new ReentrantLockMethodTest2();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test2.testMethod();
            }
        };

        for(int i = 0; i < 10; i++){

            Thread thread = new Thread(runnable);
            thread.setName(String.valueOf(i));
            thread.start();
        }
    }
}

运行结果


        3. int getWaitQueueLength(Condition condition)

            Returns an estimate of the number of threads waiting on the given condition associated with this lock.

            返回与此锁定相关的约定condition的线程预估数。

public class ReentrantLockMethodTest3 {

    private ReentrantLock lock = new ReentrantLock();
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();

    public void awaitA(){
        try {
            lock.lock();
            conditionA.await();
        } catch (InterruptedException e){
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signalA(){
        try{
            lock.lock();
            // 唤醒所有ConditionA的线程前打印等待线程数
            System.out.println("等待Condition A 的线程数:" + lock.getWaitQueueLength(conditionA));
            conditionA.signalAll();
        }finally {
            lock.unlock();
        }

    }

    public void awaitB(){
        try {
            lock.lock();
            conditionB.await();
        } catch (InterruptedException e){
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signalB(){
        try{
            lock.lock();
            // 唤醒所有ConditionB的线程前打印等待线程数
            System.out.println("等待Condition B 的线程数:" + lock.getWaitQueueLength(conditionB));
            conditionB.signalAll();
        }finally {
            lock.unlock();
        }

    }

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

        ReentrantLockMethodTest3 test3 = new ReentrantLockMethodTest3();
        Runnable runnableA = new Runnable() {
            @Override
            public void run() {
                test3.awaitA();
            }
        };

        Runnable runnableB = new Runnable() {
            @Override
            public void run() {
                test3.awaitB();
            }
        };

        for(int i = 0; i < 6; i++){

            Thread thread = new Thread(runnableA);
            thread.start();
        }

        for(int i = 0; i < 3; i++){

            Thread thread = new Thread(runnableB);
            thread.start();
        }

        Thread.sleep(2000);
        // 唤醒给定条件是ConditionA的线程
        test3.signalA();
        Thread.sleep(2000);
        // 唤醒给定条件是ConditionB的线程
        test3.signalB();
    }
}
运行结果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值