重入锁ReentrantLock常用方法(二)

重入锁ReentrantLock常用方法

1. boolean hasQueuedThread(Thread thread)

    Queries whether the given thread is waiting to acquire this lock

    查询当前线程是否在等待获取此锁

    boolean hasQueuedThreads()

    Queries whether any threads are waiting to acquire this lock.

    查询是否有线程在等待获取此锁定

    

public class ReentrantLockMethodTest4 {

    public ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void awaitLock(){
        try{
            lock.lock();
            System.out.println("Thread---" + Thread.currentThread().getName() + "---Get Lock");
            // 当前线程沉睡,占有对象锁
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e){
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

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

        ReentrantLockMethodTest4 test4 = new ReentrantLockMethodTest4();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test4.awaitLock();
            }
        };

        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.start();
        threadB.start();

        Thread.sleep(1000);
        System.out.println("Thread A 是否在等待此锁定:" + test4.lock.hasQueuedThread(threadA));
        System.out.println("Thread B 是否在等待此锁定:" + test4.lock.hasQueuedThread(threadB));
        System.out.println("是否有线程在等待此锁定:" + test4.lock.hasQueuedThreads());
    }
}

运行结果



2. boolean hasWaiters(Condition condition)

    Queries whether any threads are waiting on the given condition associated with this lock.

    查询是否存在指定Condition的线程正在等待此锁定。

public class ReentrantLockMethodTest5 {

    public ReentrantLock lock = new ReentrantLock();
    public Condition condition = lock.newCondition();

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

    public void signalMethod(){
        try{
            lock.lock();
            System.out.println("是否存在指定Condition的线程正在等待此锁定:" + lock.hasWaiters(condition)
                             + ",数量是:" + lock.getWaitQueueLength(condition));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

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

        ReentrantLockMethodTest5 test5 = new ReentrantLockMethodTest5();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test5.awaitMethod();
            }
        };

        for (int i = 0; i < 10; i++){
            Thread thread = new Thread(runnable);
            thread.start();
        }

        Thread.sleep(1000);
        test5.signalMethod();
    }
}

运行结果


3. boolean isFair()

    Returns true if this lock has fairness set true.

    如果是公平锁的话返回true(ReentrantLock默认使用的是非公平锁)

public class ReentrantLockMethodTest6 {

    private ReentrantLock lock;

    public ReentrantLockMethodTest6(boolean lockType){
        super();
        lock = new ReentrantLock(lockType);
    }

    public void awaitMethod(){
        try{
            lock.lock();
            System.out.println("是不是公平锁:" + lock.isFair());
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){

        ReentrantLockMethodTest6 test6 = new ReentrantLockMethodTest6(true);
//        ReentrantLockMethodTest6 test6 = new ReentrantLockMethodTest6(false);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test6.awaitMethod();
            }
        };

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值