Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/java/concurrent/java-concurrent-questions-02.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,14 @@ public class SynchronizedDemo {

### 可中断锁和不可中断锁有什么区别?

- **可中断锁**:获取锁的过程中可以被中断,不需要一直等到获取锁之后 才能进行其他逻辑处理。`ReentrantLock` 就属于是可中断锁。
- **不可中断锁**:一旦线程申请了锁,就只能等到拿到锁以后才能进行其他的逻辑处理。 `synchronized` 就属于是不可中断锁。
它们的区别在于:**线程在获取锁的过程中被阻塞时,是否能够因为中断而提前放弃等待。**

- **不可中断锁**:线程在等待锁期间即使收到中断信号,也不会退出阻塞状态,而是一直等待直到获得锁。中断状态会被保留,但不会影响锁的获取过程。
- `synchronized` 属于典型的不可中断锁。
- `ReentrantLock#lock()` 也是不可中断的。
- **可中断锁**:线程在等待锁的过程中如果收到中断信号,会立即停止等待并抛出 `InterruptedException`,从而有机会进行取消或错误处理。
- `ReentrantLock#lockInterruptibly()` 实现了可中断锁。
- `ReentrantLock#tryLock(long time, TimeUnit unit)` (带超时的尝试获取)也是可中断的。

## ReentrantReadWriteLock

Expand Down