File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -646,7 +646,7 @@ public class SynchronizedDemo {
646646
647647相比` synchronized ` ,` ReentrantLock ` 增加了一些高级功能。主要来说主要有三点:
648648
649- - ** 等待可中断** : ` ReentrantLock ` 提供了一种能够中断等待锁的线程的机制,通过 ` lock.lockInterruptibly() ` 来实现这个机制。也就是说当前线程在等待获取锁的过程中,如果其他线程中断当前线程「 ` interrupt() ` 」,当前线程就会跑出 ` InterruptedException ` 异常,可以捕捉该异常进行相应处理。
649+ - ** 等待可中断** : ` ReentrantLock ` 提供了一种能够中断等待锁的线程的机制,通过 ` lock.lockInterruptibly() ` 来实现这个机制。也就是说当前线程在等待获取锁的过程中,如果其他线程中断当前线程「 ` interrupt() ` 」,当前线程就会抛出 ` InterruptedException ` 异常,可以捕捉该异常进行相应处理。
650650- ** 可实现公平锁** : ` ReentrantLock ` 可以指定是公平锁还是非公平锁。而` synchronized ` 只能是非公平锁。所谓的公平锁就是先等待的线程先获得锁。` ReentrantLock ` 默认情况是非公平的,可以通过 ` ReentrantLock ` 类的` ReentrantLock(boolean fair) ` 构造方法来指定是否是公平的。
651651- ** 可实现选择性通知(锁可以绑定多个条件)** : ` synchronized ` 关键字与` wait() ` 和` notify() ` /` notifyAll() ` 方法相结合可以实现等待/通知机制。` ReentrantLock ` 类当然也可以实现,但是需要借助于` Condition ` 接口与` newCondition() ` 方法。
652652- ** 支持超时** :` ReentrantLock ` 提供了 ` tryLock(timeout) ` 的方法,可以指定等待获取锁的最长等待时间,如果超过了等待时间,就会获取锁失败,不会一直等待。
You can’t perform that action at this time.
0 commit comments