Skip to content

Commit 61555c2

Browse files
authored
Merge pull request #2769 from chrisis58/main
fix: 修改可中断锁和不可中断锁区别的描述
2 parents 04b7d9b + 3cac4ea commit 61555c2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

docs/java/concurrent/java-concurrent-questions-02.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,14 @@ public class SynchronizedDemo {
762762
763763
### 可中断锁和不可中断锁有什么区别?
764764
765-
- **可中断锁**:获取锁的过程中可以被中断,不需要一直等到获取锁之后 才能进行其他逻辑处理。`ReentrantLock` 就属于是可中断锁。
766-
- **不可中断锁**:一旦线程申请了锁,就只能等到拿到锁以后才能进行其他的逻辑处理。 `synchronized` 就属于是不可中断锁。
765+
它们的区别在于:**线程在获取锁的过程中被阻塞时,是否能够因为中断而提前放弃等待。**
766+
767+
- **不可中断锁**:线程在等待锁期间即使收到中断信号,也不会退出阻塞状态,而是一直等待直到获得锁。中断状态会被保留,但不会影响锁的获取过程。
768+
- `synchronized` 属于典型的不可中断锁。
769+
- `ReentrantLock#lock()` 也是不可中断的。
770+
- **可中断锁**:线程在等待锁的过程中如果收到中断信号,会立即停止等待并抛出 `InterruptedException`,从而有机会进行取消或错误处理。
771+
- `ReentrantLock#lockInterruptibly()` 实现了可中断锁。
772+
- `ReentrantLock#tryLock(long time, TimeUnit unit)` (带超时的尝试获取)也是可中断的。
767773
768774
## ReentrantReadWriteLock
769775

0 commit comments

Comments
 (0)