简介
针对某些简单场景,如单应用内部数字编号生成逻辑,实现一个简单通用的本地锁。
用以在单应用内部对需要同步的资源对象进行线程安全处理。
设计
获取锁
程序流程图:
说明:
- 若超过设置的获取锁超时时间,则获取到锁。
核心代码片段
获取锁
1 | public void get(long timeout) { |
释放锁
1 | public void release() { |
用法示例
1 | public class LockTest { |
输出结果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
45
46
47
48
49
50data: 47 thread: 4
data: 49 thread: 1
data: 48 thread: 8
data: 46 thread: 9
data: 45 thread: 2
data: 44 thread: 6
data: 43 thread: 7
data: 42 thread: 3
data: 41 thread: 0
data: 40 thread: 5
data: 39 thread: 4
data: 37 thread: 2
data: 38 thread: 9
data: 36 thread: 8
data: 35 thread: 1
data: 34 thread: 6
data: 33 thread: 7
data: 32 thread: 3
data: 31 thread: 0
data: 30 thread: 5
data: 29 thread: 4
data: 27 thread: 9
data: 28 thread: 2
data: 26 thread: 8
data: 25 thread: 7
data: 24 thread: 6
data: 23 thread: 1
data: 22 thread: 3
data: 21 thread: 0
data: 20 thread: 5
data: 19 thread: 4
data: 18 thread: 9
data: 17 thread: 2
data: 16 thread: 7
data: 15 thread: 8
data: 14 thread: 3
data: 12 thread: 6
data: 13 thread: 1
data: 11 thread: 0
data: 10 thread: 5
data: 9 thread: 4
data: 8 thread: 9
data: 7 thread: 2
data: 6 thread: 7
data: 5 thread: 8
data: 3 thread: 1
data: 4 thread: 6
data: 2 thread: 0
data: 1 thread: 3
data: 0 thread: 5