File tree Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -334,6 +334,7 @@ future.cancel(true);
334334
335335Java 提供了两种锁机制来控制多个线程对共享资源的互斥访问,第一个是 JVM 实现的 synchronized,而另一个是 JDK 实现的 ReentrantLock。
336336
337+
337338### synchronized
338339
339340** 1. 同步一个代码块**
@@ -489,6 +490,15 @@ public static void main(String[] args) {
489490
490491
491492### 比较
493+ | ** 比较点** | ** synchronized** | ** ReentrantLock** |
494+ | ------------------------------------------------------------ | ------------------------------------------------------------ | ---------------------------------------------- |
495+ | ** 锁的实现** | JVM实现 | JDK实现 |
496+ | ** 性能** | 新版本Java已优化很多,例如自旋锁等,性能与ReentrantLock大致相同 | 与ReentrantLock大致相同 |
497+ | ** 等待是否可中断** (当持有锁的线程长期不释放锁的时候,正在等待的线程可以选择放弃等待,改为处理其他事情。) | 不可中断 | 可中断 |
498+ | ** 公平锁** (指多个线程在等待同一个锁时,必须按照申请锁的时间顺序来依次获得锁) | 非公平 | 默认非公平,但也可以是公平的 |
499+ | ** 锁绑定多个条件** | | 一个ReentrantLock可以同时绑定多个Condition对象 |
500+
501+
492502
493503** 1. 锁的实现**
494504
You can’t perform that action at this time.
0 commit comments