We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 04b7d9b + 3cac4ea commit 61555c2Copy full SHA for 61555c2
docs/java/concurrent/java-concurrent-questions-02.md
@@ -762,8 +762,14 @@ public class SynchronizedDemo {
762
763
### 可中断锁和不可中断锁有什么区别?
764
765
-- **可中断锁**:获取锁的过程中可以被中断,不需要一直等到获取锁之后 才能进行其他逻辑处理。`ReentrantLock` 就属于是可中断锁。
766
-- **不可中断锁**:一旦线程申请了锁,就只能等到拿到锁以后才能进行其他的逻辑处理。 `synchronized` 就属于是不可中断锁。
+它们的区别在于:**线程在获取锁的过程中被阻塞时,是否能够因为中断而提前放弃等待。**
+
767
+- **不可中断锁**:线程在等待锁期间即使收到中断信号,也不会退出阻塞状态,而是一直等待直到获得锁。中断状态会被保留,但不会影响锁的获取过程。
768
+ - `synchronized` 属于典型的不可中断锁。
769
+ - `ReentrantLock#lock()` 也是不可中断的。
770
+- **可中断锁**:线程在等待锁的过程中如果收到中断信号,会立即停止等待并抛出 `InterruptedException`,从而有机会进行取消或错误处理。
771
+ - `ReentrantLock#lockInterruptibly()` 实现了可中断锁。
772
+ - `ReentrantLock#tryLock(long time, TimeUnit unit)` (带超时的尝试获取)也是可中断的。
773
774
## ReentrantReadWriteLock
775
0 commit comments