java 多线程: Thread 锁ReentrantLock 常用方法

ReentrantLock 常用方法

  1. getHoldCount获取当前线程被锁定的次数,如果调用unlock,锁定次数会减去1:
  2. lock.getQueueLength()  等待获取该锁的线程的个数
  3. lock.getWaitQueueLength(condition) 获取某个lock下的所有condition被调用await的个数,也就是暂停状态等待被唤醒的线程个数。
  4. lock.hasQueuedThread(t2) 判断某个线程是否正在等待该锁
  5. lock.hasWaiters(condition) 判断是否有线程用了本锁下面的Condition await等待中
  6. isFair():判断当前锁是不是公平锁
  7. isHeldByCurrentThread():判断该锁是否锁定了当前线程
  8. isLocked():判断该锁是否锁定了任意一个线程
  9. tryLock(long timeOut, TimeUtil timeUtil):该锁如果在规定的时间内没有被其他线程占有,那么直接对当前线程加锁
  10. Condition.awaitUninterruptibly():在某个线程中使用Condition的await方法之后直接调用thread.interrupt()方法会报错。但是如果我们使用awaitUninterruptibly方法依然会起到等待的作用,但是在调用thread.interrupt()方法不会报错
  11. condition.awaitUntil(Date deadline) 在等待时间之内可以被其它线程唤醒,等待时间一过该线程会自动唤醒,和别的线程争抢锁资源,只不过这里设置的是一个到期的具体时间。用法和condition.wait(long)没有任何区别

3、查看对应condition.await()状态的线程个数  lock.getWaitQueueLength(condition)

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

/**
 * @ClassName getAwaitCount
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/25.
 */
public class ReentrantLockgetAwaitCount {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Runnable r = () -> {
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        };
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            threads.add(new Thread(r));
        }
        threads.forEach(thread -> thread.start());
        Thread.sleep(2000);
        lock.lock();
        System.out.println("目前有" + lock.getWaitQueueLength(condition) + "个线程执行了对应的condition的await方法"); //目前有10个线程执行了对应的condition的await方法
        condition.signalAll();
        System.out.println("目前有" + lock.getWaitQueueLength(condition) + "个线程执行了对应的condition的await方法"); //目前有0个线程执行了对应的condition的await方法
        lock.unlock();
    }
}

 

4、判断某个线程是否正在等待该锁 lock.hasQueuedThread(t2)

示例代码:

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName ReentrantLockWaitLockOrNot
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/25.
 */
public class ReentrantLockWaitLockOrNot {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Runnable r = () -> {
            lock.lock();
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        };
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        Thread.sleep(1000);
        t2.start();
        Thread.sleep(1000);
        System.out.println(lock.hasQueuedThread(t2)); //true
    }
}

5、判断是否有线程在对应锁的waiter暂停状态中 lock.hasWaiters(condition)

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

/**
 * @ClassName ReentrantLockHasWaiters
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/25.
 */
public class ReentrantLockHasWaiters {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Runnable r = () -> {
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        };
        Thread t1 = new Thread(r);
        t1.start();
        Thread.sleep(1000);
        lock.lock();
        System.out.println(lock.hasWaiters(condition)); //true
        condition.signal();
        Thread.sleep(1000);
        System.out.println(lock.hasWaiters(condition)); //false
        lock.unlock();
    }
}

 

 

上一篇:学生党使用服务器要努力呀


下一篇:小白勿进!利用多condition动态管理池化的异步资源背景,深夜思考