分布式锁的isFair()、isHeldByCurrentThread()和isLocked()的用法和区别

方法isFair()、isHeldByCurrentThread()和isLocked()的用法和区别

1、lock.isFair()方法用来判断lock锁是公平锁还是非公平锁。公平锁是指,线程获得锁的顺序是按其等待锁的先后顺序来的,先来先获得FIFO。反之,非公平锁则是线程随机获得锁的,lock默认是非公平锁。

2、lock.isHeldByCurrentThread()的作用是查询当前线程是否保持此锁定,和lock.hasQueueThread()不同的地方是:lock.hasQueueThread(Thread thread)的作用是判断当前线程是否处于等待lock的状态。

3、lock.isLocked()的作用是查询此锁定是否由任意线程保持。

方法isFair()、isHeldByCurrentThread()和isLocked()的用法示例

ServiceIsFair.java

/**
 * lock.isFair()方法用来判断lock锁是公平锁还是非公平锁
 */
public class ServiceIsFair {
    public ReentrantLock lock;
 
    public ServiceIsFair(Boolean isFair) {
        this.lock = new ReentrantLock(isFair);
    }
 
    public void serviceMethod(){
        try {
            lock.lock();
            System.out.println("公平锁情况:"+lock.isFair());
        } finally {
            lock.unlock();
        }
    }
}

ServiceIsHeldByCurrentThread.java

 
/**

* lock.isHeldByCurrentThread()的作用是查询当前线程是否保持此锁定

* 和lock.hasQueueThread()不同的地方是:lock.hasQueueThread(Thread thread)的作用是判断当前线程是否处于等待lock的状态

*/

public class ServiceIsHeldByCurrentThread {

    private ReentrantLock lock;


    public ServiceIsHeldByCurrentThread(Boolean isFair) {

        this.lock = new ReentrantLock(isFair);

    }


    public void serviceMethod(){

        try {
            System.out.println("lock.isHeldByCurrentThread()"+lock.isHeldByCurrentThread());

            lock.lock();

            System.out.println("lock.isHeldByCurrentThread() "+lock.isHeldByCurrentThread());

        } finally {

            lock.unlock();

        }

    }

}

ServiceIsLocked.java

/**

* lock.isLocked()的作用是查询此锁定是否由任意线程保持

*/

public class ServiceIsLocked {

    private ReentrantLock lock;


    public ServiceIsLocked(Boolean isFair) {

        this.lock = new ReentrantLock(isFair);

    }


    public void serviceMethod(){

        try {

            System.out.println("lock.isLocked() "+lock.isLocked());

            lock.lock();

            System.out.println("lock.isLocked() "+lock.isLocked());

        } finally {

            lock.unlock();

        }

    }

}

Run.java



public class Run {

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

        final ServiceIsFair serviceIsFair0 = new ServiceIsFair(true);

        final ServiceIsFair serviceIsFair1 = new ServiceIsFair(false);

        Runnable runnable0 = new Runnable() {

            @Override

            public void run() {

                serviceIsFair0.serviceMethod();

            }

    };

    Runnable runnable1 = new Runnable() {

        @Override

        public void run() {

            serviceIsFair1.serviceMethod();

        }

    };

    Thread thread0 = new Thread(runnable0);

    thread0.start();

    Thread thread1 = new Thread(runnable1);

    thread1.start();



    final ServiceIsHeldByCurrentThread serviceIsHeldByCurrentThread = new         ServiceIsHeldByCurrentThread(true);

    Runnable runnable2 = new Runnable() {

        @Override

        public void run() {

            serviceIsHeldByCurrentThread.serviceMethod();

        }

    };

    Thread thread2 = new Thread(runnable2);

    thread2.start();




    final ServiceIsLocked serviceIsLocked = new ServiceIsLocked(true);

    Runnable runnable3 = new Runnable() {

        @Override

        public void run() {

            serviceIsLocked.serviceMethod();

        }

    };

    Thread thread3 = new Thread(runnable3);

    thread3.start();

    }

}
运行结果:
创建了两个锁,公平锁和非公平锁,所以lock.isFair()一个为true一个为false。
在lock.lock()执行之前线程未获得锁,所以lock.isHeldByCurrentThread()结果为false,执行lock.lock()之后结果为true。
在lock.lock()执行之前没有线程获得锁,所以lock.lock()为false;
在执行lcok.lock()之后,当前有线程已经获得了锁,所以lock.lsLocked()为true。

你可能感兴趣的:(java,java)