File tree 1 file changed +2
-2
lines changed
1 file changed +2
-2
lines changed Original file line number Diff line number Diff line change @@ -542,7 +542,7 @@ public class SynchronizedDemo {
542
542
相比` synchronized ` ,` ReentrantLock ` 增加了一些高级功能。主要来说主要有三点:
543
543
544
544
- ** 等待可中断** : ` ReentrantLock ` 提供了一种能够中断等待锁的线程的机制,通过 ` lock.lockInterruptibly() ` 来实现这个机制。也就是说正在等待的线程可以选择放弃等待,改为处理其他事情。
545
- - ** 可实现公平锁** : ` ReentrantLock ` 可以指定是公平锁还是非公平锁。而` synchronized ` 只能是非公平锁。所谓的公平锁就是先等待的线程先获得锁。` ReentrantLock ` 默认情况是非公平的,可以通过 ` ReentrantLock ` 类的` ReentrantLock(boolean fair) ` 构造方法来制定是否是公平的 。
545
+ - ** 可实现公平锁** : ` ReentrantLock ` 可以指定是公平锁还是非公平锁。而` synchronized ` 只能是非公平锁。所谓的公平锁就是先等待的线程先获得锁。` ReentrantLock ` 默认情况是非公平的,可以通过 ` ReentrantLock ` 类的` ReentrantLock(boolean fair) ` 构造方法来指定是否是公平的 。
546
546
- ** 可实现选择性通知(锁可以绑定多个条件)** : ` synchronized ` 关键字与` wait() ` 和` notify() ` /` notifyAll() ` 方法相结合可以实现等待/通知机制。` ReentrantLock ` 类当然也可以实现,但是需要借助于` Condition ` 接口与` newCondition() ` 方法。
547
547
548
548
如果你想使用上述功能,那么选择 ` ReentrantLock ` 是一个不错的选择。
@@ -707,4 +707,4 @@ Atomic 原子类部分的内容我单独写了一篇文章来总结:[Atomic
707
707
- 理解 Java 中的 ThreadLocal - 技术小黑屋:https://droidyue.com/blog/2016/03/13/learning-threadlocal-in-java/
708
708
- ThreadLocal (Java Platform SE 8 ) - Oracle Help Center:https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html
709
709
710
- <!-- @include: @article-footer.snippet.md -->
710
+ <!-- @include: @article-footer.snippet.md -->
You can’t perform that action at this time.
0 commit comments