一, Lock 锁接口
定义 Java 中 Java.Util.concurrent 中锁 Lock 的基本接口方法
二, Lock 接口方法
Lock 接口有以下几个方法
- public interface Lock {
- void lock();
- void lockInterruptibly() throws InterruptedException;
- boolean tryLock();
- boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
- void unlock();
- Condition newCondition();
- }
lock() 是一直等到获取锁
void lock();
tryLock() 是尝试获取锁
boolean tryLock();
tryLock(Long time, TimeUnit time) 设置尝试在某个时间内获取锁, 过了这个时间就不尝试获取了
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
lockInterruptibly 中止锁
void lockInterruptibly() throws InterruptedException;
unlock( ) 解锁
unlock();
newCondition 稍后解释
Condition newCondition();
三, lock 使用
Lock 的使用, 通过实现 Lock 接口的 ReentranLock 类来实现;
来源: http://www.bubuko.com/infodetail-3654071.html