多线程之ReentrantLock的基本方法使用(十七)

1.getHoldCount方法

返回当前线程调用lock()方法的次数

package com.dome.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 * getHoldCount
 * @date 2021/3/11 14:16
 */
public class Test02 {


    static ReentrantLock lock = new ReentrantLock();

    public static void m1(){
        try{
            lock.lock();
            //打印调用lock的次数
            System.out.println(Thread.currentThread().getName()+"--hold count"+lock.getHoldCount());
            //调用m2,ReentrantLock是可重入锁
            m2();
        }finally {
            lock.unlock();
        }
    }

    public static void m2(){
        try{
            lock.lock();
            //打印调用lock的次数
            System.out.println(Thread.currentThread().getName()+"==hold count"+lock.getHoldCount());
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {

        m1();
    /*
    main--hold count1
    main==hold count
     */
    }


}


在这里插入图片描述

二、getQueueLength方法

返回正等待获得锁的预估数

package com.dome.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 * @date 2021/3/11 14:22
 */
public class Test03 {

    static ReentrantLock lock = new ReentrantLock();

    public static void sm(){
        try{
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"获得锁,执行方法,估计等待获得锁的线程数:" +lock.getQueueLength());
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }


    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Test03.sm();
            }
        };
        //开启10个线程
        for (int i = 0; i < 10; i++) {
            new Thread(r).start();
        }
    }

}


在这里插入图片描述

三、 getWaitQueueLength

返回与Condition条件相关的等待的线程预估数


package com.dome.lock.method;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 * getWaitQueueLength   返回与Condition条件相关的等待的线程预估数
 * @date 2021/3/11 14:29
 */
public class Test04 {

    static class Service{

        private ReentrantLock lock = new ReentrantLock();

        private Condition condition = lock.newCondition();  //

        public void waitMethod(){
            try{
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"进入等待前,现在该condition条件上等待的线程预估数"+
                        lock.getWaitQueueLength(condition));
                condition.await();
            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }

        public void notifyMethod(){

            try {
                lock.lock();
                condition.signalAll();
                System.out.println("唤醒所有等待后,condition条件上等待的线程预估数"+lock.getWaitQueueLength(condition));
            }finally {
                lock.unlock();
            }

        }

    }

    public static void main(String[] args) throws InterruptedException {
        Service service = new Service();
        Runnable r = new Runnable() {
            @Override
            public void run() {
                service.waitMethod();
            }
        };
        //创建10个线程
        for (int i = 0; i < 10; i++) {
            new Thread(r).start();
        }
        Thread.sleep(1000);
        service.notifyMethod();
    }

}

在这里插入图片描述

四、hasQueuedThread

查询参数指定的线程是否在等待获得锁

hasQueuedThreads 查询是否还有线程等待获得该锁

package com.dome.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 *
 * hasQueuedThread  查询参数指定的线程是否在等待获得锁
 *
 * hasQueuedThreads 查询是否还有线程等待获得该锁
 * @date 2021/3/11 14:38
 */
public class Test05 {

        static ReentrantLock lock = new ReentrantLock();

        public static void waitMethod(){
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "获得了锁对象");
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                System.out.println(Thread.currentThread().getName() + "释放了锁对象----");
                lock.unlock();

            }
        }

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

            Runnable r = new Runnable() {
                @Override
                public void run() {
                    Test05.waitMethod();
                }
            };

            Thread[] threads = new Thread[6];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(r);
            threads[i].setName("thread - "+i);
            threads[i].start();
        }

        Thread.sleep(3000);
        //判断数组中的每个线程对象是否正在等待获得锁
        System.out.println(lock.hasQueuedThread(threads[0]));
        System.out.println(lock.hasQueuedThread(threads[1]));
        System.out.println(lock.hasQueuedThread(threads[2]));
        System.out.println(lock.hasQueuedThread(threads[3]));
        System.out.println(lock.hasQueuedThread(threads[4]));

        Thread.sleep(2000);
        //判断是否还有线程在等待获得该锁
        System.out.println(lock.hasQueuedThreads());

    }


}

在这里插入图片描述

五、hasWaiters

查询是否有线程正在等待指定的condition条件

package com.dome.lock.method;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 *
 * hasWaiters  查询是否有线程正在等待指定的condition条件
 * @date 2021/3/11 14:49
 */
public class Test06 {

    static ReentrantLock lock = new ReentrantLock();  //锁

    static Condition condition = lock.newCondition();  //返回锁  条件

    static void  sm(){
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "lock .....");
            System.out.println("是否有线程正在等待当点condition条件?"+lock.hasWaiters(condition) +"--等待数量: "
                    +lock.getWaitQueueLength(condition));
            //超时后自动唤醒
            condition.await(new Random().nextInt(1000), TimeUnit.MILLISECONDS);

        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            System.out.println(Thread.currentThread().getName() +"超时唤醒,是否有线程正在等待当点condition条件?"+lock.hasWaiters(condition) +"--等待数量: "
                    +lock.getWaitQueueLength(condition));
            System.out.println(Thread.currentThread().getName() + "unlock...");
            lock.unlock();
        }
    }

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            @Override
            public void run() {
                sm();
            }
        };

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


    }


}

在这里插入图片描述

六、isHeldByCurrentThread

.isFair() 判断是否为公平锁
判断当前线程是否持有锁

package com.dome.lock.method;

import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 *
 *
 * .isFair()  判断是否为公平锁
 *
 * .isHeldByCurrentThread判断当前线程是否持有锁
 * @date 2021/3/11 15:01
 */
public class Test07 {


    static class Service{
        private ReentrantLock lock;

        public Service(boolean isFair){
            this.lock = new ReentrantLock(isFair);
        }

        public void ServiceMethod(){

            try{
                System.out.println("是否公平锁? "+lock.isFair() + "---" +Thread.currentThread().getName() +
                        "调用lock前是否持有锁? "+lock.isHeldByCurrentThread());
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "调用lock方法后是否持有锁? "+lock.isHeldByCurrentThread());
            }finally {
                if(lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }

        }

    }

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            @Override
            public void run() {
                int num = new Random().nextInt();

                new Service(num % 2 == 0 ? true : false).ServiceMethod();
            }
        };

        for (int i = 0; i < 3; i++) {
            new Thread(r,"thread-"+i).start();
        }

    }

}

在这里插入图片描述

七、 isLocked() 判断当前锁是否被线程持有

package com.dome.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qb
 * @version 1.0
 * @Description
 * isLocked() 判断当前锁是否被线程持有
 *
 * @date 2021/3/11 15:09
 */
public class Test08 {

    static ReentrantLock lock = new ReentrantLock();

    static void sm(){

        try {
            System.out.println("lock方法之前--" + lock.isLocked());
            lock.lock();
            System.out.println("lock方法之后--" + lock.isLocked());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("11 lock 是否被线程持有?---"+lock.isLocked());

        new Thread(new Runnable() {
            @Override
            public void run() {
                sm();
            }
        }).start();

        Thread.sleep(3000);

        System.out.println("22 lock 是否被线程持有?---"+lock.isLocked());
    }

}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值