java-Lock和ReentrantLock显示锁

1、Lock提供了无条件的、可轮询的、定时的、可中断的锁获取操作,所有加锁和解锁的方法都是显式的。

public interface Lock{

    voidlock();

    voidlockInterruptibly() throws InterruptedException;

    booleantryLock();

    booleantryLock(long timeout,TimeUnit unit) throwsInterruptedException;

    voidunlock();

    ConditionnewCondition();

}

2、ReentrantLock实现了lock接口,跟synchronized相比,ReentrantLock为处理不可用的锁提供了更多灵活性。

3、使用lock接口的规范形式要求在finally块中释放锁lock.unlock()。如果锁守护的代码在try块之外抛出了异常,它将永远不会被释放。

你可能感兴趣的:(java)