重入锁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();
        }
    }
}

运行结果

重入锁ReentrantLock常用方法(一)_第1张图片

        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();
    }
}
运行结果

重入锁ReentrantLock常用方法(一)_第2张图片


你可能感兴趣的:(多线程编程)