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







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