Locks包 类层次结构
lock接口的方法和描述
方法签名 | 描述 |
---|---|
void lock(); | 获取锁(不死不休) |
boolean trylock(); | 获取锁(浅尝辄止) |
boolean tryLock(long time,TimeUnit unit) throws InterruptedException; | 获取锁(过时不候) |
void lockInterruptibly() throws InterruptedException; | 获取锁(任人摆布) |
void unlock(); | 释放锁 |
Condition newCondition(); | 可以把它看作一个监视器 |
condition常用的方法有:
Condition.await()
:线程挂起Condition.signal()
:线程唤醒
结论:
lock()
最常用;lockInterruptibly()
方法一般更昂贵,有的impl可能没有实现lockInterruptibly()
,只有真的需要效应中断时,才使用,使用之前看看impl对该方法的描述。