-
Notifications
You must be signed in to change notification settings - Fork 20
/
BackoffLock.java
44 lines (35 loc) · 1.03 KB
/
BackoffLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.github.mottox.taomp.concurrent.locks;
import java.util.concurrent.atomic.AtomicBoolean;
import com.github.mottox.taomp.common.Backoff;
/**
* 使用了{@link com.github.mottox.taomp.common.Backoff}的锁。
*/
public class BackoffLock implements SimpleLock {
private AtomicBoolean state = new AtomicBoolean(false);
private static final int MIN_DELAY = 50;
private static final int MAX_DELAY = 80;
@Override
public void lock() {
Backoff backoff = new Backoff(MIN_DELAY, MAX_DELAY);
while (true) {
// 锁已经被占用,则等待
while (state.get()) {
}
// 成功占有锁
if (!state.getAndSet(true)) {
return;
} else {
// 后退
try {
backoff.backoff();
} catch (InterruptedException e) {
// 留空
}
}
}
}
@Override
public void unlock() {
state.set(false);
}
}