Skip to content

Commit 532229d

Browse files
author
zhangquanli
committed
程序清单 13-5 可中断的锁获取操作
1 parent a24aa07 commit 532229d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package chapter_13.code_05;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* 程序清单 13-5 可中断的锁获取操作
8+
*/
9+
public class InterruptibleLocking {
10+
private Lock lock = new ReentrantLock();
11+
12+
public boolean sendOnSharedLine(String message) throws InterruptedException {
13+
lock.lockInterruptibly();
14+
try {
15+
return cancellableSendOnSharedLine(message);
16+
} finally {
17+
lock.unlock();
18+
}
19+
}
20+
21+
private boolean cancellableSendOnSharedLine(String message) throws InterruptedException {
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)