重入锁ReentrantLock常用方法(三)

1. isHeldByCurrentThread()

    查询当前线程是否保持锁定。

    isLocked()

    查询是否存在任意线程保持此锁定。

public class ReentrantLockMethodTest7 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){
        try{
            System.out.println("isHeldByCurrentThread:" + lock.isHeldByCurrentThread());
            System.out.println("isLocked:" + lock.isLocked());
            lock.lock();
            System.out.println("isHeldByCurrentThread:" + lock.isHeldByCurrentThread());
            System.out.println("isLocked:" + lock.isLocked());
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){

        ReentrantLockMethodTest7 test7 = new ReentrantLockMethodTest7();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test7.testMethod();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

运行结果,

isHeldByCurrentThread,在使用重入锁进行锁定后,当前线程保持锁定状态。

isLocked,使用重入锁进行锁定后,存在一个线程保持此锁定。

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


2. lockInterruptbly()

    如果当前线程未被中断,则获取锁定;如果已中断,则抛出异常(InterruptedException)

public class ReentrantLockMethodTest8 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){

        try{
            lock.lockInterruptibly();
            for (int i = 0; i < 10; i++){
                System.out.println("Thread---" + Thread.currentThread().getName() + "保持锁定!");
            }
        } catch (InterruptedException e){
            System.out.println("Thread---" + Thread.currentThread().getName() + "异常!");
            e.printStackTrace();
        } finally{
            lock.unlock();
        }
    }

    public static void main(String[] args){
        ReentrantLockMethodTest8 test8 = new ReentrantLockMethodTest8();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test8.testMethod();
            }
        };
        // A线程正常运行
        Thread threadA = new Thread(runnable);
        threadA.setName("A");
        threadA.start();

        Thread threadB = new Thread(runnable);
        threadB.setName("B");
        threadB.start();
        // B线程标记中断
        threadB.interrupt();
    }
}

运行结果

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


3. tryLock()

    尝试获取锁定,如果锁定没有被别的线程保持,则获取锁定,即成功获取返回true,否则返回false(锁定已被别的线程获取)

    tryLock(long timeout, TimeUnit unit)

    尝试获取锁定,如果锁定没有被别的线程保持,则获取锁定,即成功获取返回true,如果没有获取锁定,则等待指定的时间,要是在指定时间内获取锁定,返回true,否则返回false。

public class ReentrantLockMethodTest9 {

    private ReentrantLock lock = new ReentrantLock();

    public void testMethod(){
            if(lock.tryLock()){
                System.out.println("Thread---" + Thread.currentThread().getName()+"---获得锁");
            }else{
                System.out.println("Thread---" + Thread.currentThread().getName()+"---没有获得锁");
            }
        
        // 测试tryLock(long time, TimeUnit unit)
//        try{
//            if(lock.tryLock(1, TimeUnit.SECONDS)){
//                System.out.println("Thread---" + Thread.currentThread().getName()+"---获得锁");
//            }else{
//                System.out.println("Thread---" + Thread.currentThread().getName()+"---没有获得锁");
//            }
//        } catch (InterruptedException e){
//            e.printStackTrace();
//        } finally {
//            lock.unlock();
//        }

    }

    public static void main(String[] args){
        ReentrantLockMethodTest9 test9 = new ReentrantLockMethodTest9();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                test9.testMethod();
            }
        };

        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.setName("A");
        threadB.setName("B");
        threadA.start();
        threadB.start();
    }
}

运行结果,A获取锁返回true,B没有获得锁返回false


将测试tryLock(long time, TimeUnit unit)的代码注释去掉并注释掉tryLock()方法

运行结果,A获取锁定,执行完后解锁,B线程在指定的时间内获取了锁。

    

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