JUC之Lock接口和Condition接口

Lock接口

package com.sk.lock;

public interface Lock
{
    /**
     * 获取锁,如果锁不可用则线程一直等待
     * 获取失败后,线程进入等待队列自旋或休眠,直到锁可用,并且忽略中断的影响
     */
    void lock();

    /**
     * 获取锁,响应中断,如果锁不可用则线程一直等待;
     * 线程进入等待队列park后,如果线程被中断,则直接响应中断(抛出InterruptedException)
     *
     * @throws InterruptedException
     */
    void lockInterruptibly() throws InterruptedException;

    /**
     * 获取锁,获取失败直接返回,不进入等待队列
     * @return
     */
    boolean tryLock();

    /**
     * 获取锁,等待给定时间后如果获取失败直接返回
     * @param time
     * @param unit
     * @return
     * @throws InterruptedException
     */
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

    /**
     * 释放锁
     */
    void unlock();

    /**
     * 创建一个新的等待条件
     * @return
     */
    Condition newCondition();
}

Condition接口

public interface Condition
{
    /**
     * 使当前线程被唤醒或者中断之前一直处于等待状态
     * @throws InterruptedException
     */
    void await() throws InterruptedException;

    /**
     * 使当前线程在被唤醒、被中断或到达指定等待时间之前一直处于等待状态
     * @param time
     * @param unit
     * @return
     * @throws InterruptedException
     */
    boolean await(long time, TimeUnit unit) throws InterruptedException;

    /**
     * 使当前线程在被唤醒、被中断或到达指定等待时间之前一直处于等待状态
     * @param nanosTimeout
     * @return
     * @throws InterruptedException
     */
    long awaitNanos(long nanosTimeout) throws InterruptedException;

    /**
     * 使当前线程在被唤醒之前一直处于等待状态
     */
    void awaitUninterruptibly();

    /**
     * 使当前线程在被唤醒、被中断或者达到最后期限之前一直处于等待状态
     * @param deadline
     * @return
     * @throws InterruptedException
     */
    boolean awaitUntil(Date deadline) throws InterruptedException;

    /**
     * 唤醒一个等待线程
     */
    void signal();

    /**
     * 唤醒所有等待线程
     */
    void signalAll();
}
发布了280 篇原创文章 · 获赞 49 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/kaikai_sk/article/details/88697502